• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access another class's public variable in constructor  RSS feed

 
ParagM Pmahajan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have class C1 and class C2.
C1 has a public boolean variable b1.

The value of b1 is set in C1 and then I create a object of Class C2 in C1.
ie in C1 I have :

b1 = true;
C2 c2 = new C2();

Now in constructor of C2 , I want to make a decision based on the value of b1.
How can I access the value of b1 (which is a variable of Class C1) in the constructor of C2 ?
The constructor of C2 cannot have any arguements.

Thanks .
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If b1 is not declared static, and C1 is not a singleton, and you can't pass the instance of C1 as an argument to the constructor of C2, you're out of luck.
Unless you make C2 an inner class of C1, of course.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!