Click to See Complete Forum and Search --> : Global variable, and object question..


fatbob_51
08-29-2007, 11:59 AM
Hi!
Im new to java and Im trying to do something really simpel, but based on objects....

What I have done so far is this:

//Importerar färdig skrivna paket med funktioner//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class test
{


public static void main(String args[])
{
test();
}

public static void test()
{
System.out.println("hello");
}
}

But the thing is that I dont know how I'm supposed to call the object called "test"..if I even can call it and use it..since I have done this in JApplets before I thought it whould work here aswell...or something...

So to put my question easy, how do I create an object for a public void static main thing?
And I also have another question aswell, how do I "recall" variables from one object to another?, since most of my variables only seems to work within the object that its declared in...

potterd64
08-29-2007, 01:33 PM
This should fix it
//Importerar färdig skrivna paket med funktioner//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class test
{


public static void main(String args[])
{
test mytest = new test();
}

public static void test()
{
System.out.println("hello");
}
}

to make object member variables available outside the object you can make them public, or give make them private and provide accessor methods to them.

example:


public class SomeClass{

public int var1;
private int var2;
public SomeClass(){
var1 = 1;
var2 = 2;
}
//since var2 is private, other code can get the value through this method
public int getVar2(){
return var2;
}
}


then some outside code can get those values like this

SomeClass myObject = new SomeClass();
int a = myObject.var1 //since var1 is declared public, it can be accessed directly
int b = myObject.getVar2(); //var2 is private, so this public method is called to access the value


note that making class variables public is considered bad practice. Making them private is safer.

fatbob_51
08-29-2007, 01:53 PM
Thanks mate!

fatbob_51
08-29-2007, 02:20 PM
I couldnt get the variable thing to work, since I need to use the "public static void main(bla bla)" thing to run the program and Im trying to do this:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class test{

public int var1;

public static void main(String args[]){
System.out.println(var1);
}
public void lol(){
var1 = 1;
}
}

and I know...its wrong wrong and more wrong, but I just cant figure out how to call that var1 variable to the main functiuon....could someone just make me a smal application or whats so ever so I could see how you actually create an application that can call a variable between all the objects and also then load the objects in the "main" function...whould be very happy if someone could help me with this....

//EDIT

Oh and btw...I cant run the "lol" function/method since I cant call it in my main since it aint static, and if I change it to a static method then it cant call the variable called "var1" into the method...

jasonahoule
08-30-2007, 02:17 PM
Here you go. First you need to instantiate your object. Then you can call methods and access fields against it.

public class Test{

public int var1;

public static void main(String args[]){
Test myTest = new Test();
myTest.lol();
System.out.println(myTest.var1);
}
public void lol(){
var1 = 1;
}
}