classes question



Hi

I am starting my first Java program and I have a question about getting
objects to interact with each other.
In the main class I instantiate two other classes, A and B. Class A has
a public variable. But I cannot access this public variable from within
class B. If I make Class A public , the variable becomes visible from
within A, but I get a message saying that class A should be in a
separate file.

Does this mean all the classes have to be put in separate files, or is
there a way of making the variable visible to class B?

code below

cheers David



package talk;

class A {
public String hello = "Hello World";

A(){
}
}

class B {
B() {
System.out.println(a.hello);
}
}

public class test {
public test() {
}

public static void main(String[] args) {
test test = new test();
B b = new B();
A a = new A();
}
}

.



Relevant Pages

  • Re: classes question
    ... objects to interact with each other. ... In the main class I instantiate two other classes, A and B. Class A has ... separate file. ... public static void main{ ...
    (comp.lang.java.help)
  • Re: classes question
    ... objects to interact with each other. ... In the main class I instantiate two other classes, A and B. Class A has ... separate file. ... public static void main{ ...
    (comp.lang.java.help)