java - Can I call an object to another class? -


i have 2 classes, below:

public class javaapplication12 {  public static void main(string[] args) {    newclass aclass = new newclass("rabbit", "bungalow");    system.out.println(aclass.tostring()); }} 

and

public class newclass { string animal; string building;  public newclass(string a, string b){ animal=a; building=b;}  newclass class1 = new newclass("cat", "house"); newclass class2 = new newclass("dog", "shed");  @override public string tostring(){     return animal+building; }} 

so have object called aclass in javaapplication12 , have been able print out tostring of this. want able print out tostring of objects class1 , class2, in newclass, want print them in javaapplication12, since main class can run. if write

system.out.println(class1.tostring()); 

in javaapplication12, wouldn't know can't see object class1 in javaapplication12. there way print out tostring of class1 in javaapplication12?

edit

so think i've figured out can't store cat, dog, etc. information in way have - think perhaps how have managed confuse people sorry that! there way can store information in second class , able call main class i.e. want able print cat, dog in javaapplication1 store information in newclass, , store lots of other information of sort. arraylist? sorry again confusion.

im not sure question, ill try answer : have static variables , "normal" variables. when create object of class, create each normal var it, too. example :

class foo { public int a; //for each obj public static int b=3; //global var of class, acessable anywhere public foo(int a) { this.a=a; } }  system.out.println(new foo(0).a); //-> 0 

and of course there static variables, , there not each object, there in class.

system.out.println(foo.b); //-> 3 

and can have objs of same class type in class, example :

public foo f; 

solution 1 : others did suggest, can move them main method :

public class javaapplication12 {  public static void main(string[] args) {    newclass aclass = new newclass("rabbit", "bungalow");    newclass class1 = new newclass("cat", "house");    newclass class2 = new newclass("dog", "shed");    system.out.println(aclass.tostring()); }} 

and

public class newclass { string animal; string building;  public newclass(string a, string b){ animal=a; building=b;}   @override public string tostring(){     return animal+building; }} 

accessing in main :

system.out.println(class1.tostring()); 

solution 2 : make them static, can acess them main method

public class newclass { string animal; string building; public static newclass class1 = new newclass("cat", "house"); public static newclass class2 = new newclass("dog", "shed");  public newclass(string a, string b){ animal=a; building=b;}   @override public string tostring(){     return animal+building; }} 

then accessed in main:

system.out.println(newclass.class1.tostring()); 

note wont have endless recursion g_h pointed out.

solution 3 : have them each obj(only if need so)

public class newclass { string animal; string building; public newclass class1 = new newclass("cat", "house"); public newclass class2 = new newclass("dog", "shed");  public newclass(string a, string b){ animal=a; building=b;}   @override public string tostring(){     return animal+building; }} 

note have access them obj, example aclass :

system.out.println(aclass.class1.tostring()); 

that solution provides more flexibility because can have different values there different objs, example, if each animal has 2 neighbors


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -