• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access to variables in another class?  RSS feed

 
Tobi Taiwo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will like to add to the questions about constructors and its this.
i have a class A with a constructor, i have a class B which initialize the constructor. also i have a class C which needs a variable in class A but doesn't need to initialize the constructor of A. the question how do i access the variable of class A without initilizing the constructor.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobi Taiwo wrote:the question how do i access the variable of class A without initilizing the constructor.

As your question doesn't define how the variable will be used, it is difficult to give you a concrete answer. Two scenarios that could be used:

1) The variable could be defined as static, as variables that are static do not require instantiation to be accessed. Of course, all instances of A (or instances of derived classes of A) will share that same static variable, so any changes to the value of that variable will be reflected in all instances. If the variable is public, the variable could be accessed directly via A.myAVariable (assuming the variable was called myAVariable). Not a great idea, unless this variable is a constant/final value, such as Math.PI. If the variable is private, then appropriate getters and setters (allowing you to validate the input before updating) that are accessible and static would need to be defined:
2) If class C is instantiating an instance of class B, then you have implicitly instantiated an object of class A within the class B. Therefore, you can access member variables of class A via getters and setters inherited by class B:
However, there would be no way to access a member variable of Class A without either explicitly instantiating such an object (or implicitly through instantiating via a derived class) as a member variable is a variable containing an unique value associated just with that instance of the object.

Cheers!
Chris
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple: you don't.
You don't initialise constructors. You use the new operator to instantiate a class and that process calls a constructor.
You oughtn't to access variables in another class.
I suspect you have some poor design there and you haven't explained at all well what you are doing.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobi Taiwo wrote:i will like to add to the questions about constructors and its this. . .
That is an unrelated question. Fortunately I can separate that question off to make a new thread.
 
Tobi Taiwo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get the solution to this, i declare two constructor
public class A(int b){
set = b;
}
public class A(){
}
so i can access A without initializing the constructor.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that probably won't compile:
You have not sorted out the problem at all. You have simply added another problem that the value of a field in your class has not been set.
I suspect you have some bad design and need to change something completely different, but I think such a no‑arguments constructor should be deleted.
 
Tobi Taiwo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
acctually it worked as i intended it to but i may accept that my design needs revisiting but java actually allow you to create more than ine constructor in a class and then i create an empty one then its possible for me to call a variable in class A from class C
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!