Accessing values of static private variabes from other classes

Hi all,

I've 2 java classes in a package. I want to access the values of the private variables in the first class A from the second class B but the value that I get is null, can anybody help me?

Here is the code:

package Test;

class A{

private static String name;

public static void main(String args[]){

A obj = new A();

obj.setName("Abebe");

System.out.println(obj.getName());

}

public static String getName() {

return name;

}

public static void setName(String aName) {

name = aName;

}

}

//and the second file is

package Test;

public class B {

public static void main(String args[]){

A obj2 = new A();

System.out.println("Name: " + A.getName() ); // this gives me null, why ? , I was expecting "Abebe"

System.out.println("name:"+ obj2.getName()); // this gives me null 2, helppppppp? I was expecting "Abebe"

}

}

[977 byte] By [Nega] at [2007-11-27 0:13:24]
# 1

> System.out.println("Name: " + A.getName() ); // this gives me null, why ? ,

> I was expecting "Abebe"

A.getName() will return null because the static String name in class A has never

been given a value. Notice that the main() method of class A is never invoked.

> System.out.println("name:"+ obj2.getName()); // this gives me null 2, helppppppp? I

> was expecting "Abebe"

obj2.getName() is the same as A.getName() and will (therefore) return null as well.

In general if you mean A.getName() you should say A.getName() and not

obj2.getName().

pbrockway2a at 2007-7-11 21:57:05 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you for your fast response.
Nega at 2007-7-11 21:57:05 > top of Java-index,Java Essentials,Java Programming...