• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Access another class's public variable in constructor

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 .
Posts: 1952
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic