Quick Question about Variables

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
32,829
Reaction score
3,033
Location
On the toilet
Hi,

Code:
class a
{
private String answer = "ham";

public String getAnswer(){
return this.answer; }
}

Now.. I have another class B. Which way is the best way to use answer. Declare the variable or call super.getAnswer() multiple times in same class.
Code:
//example 1
String random = super.getAnswer();

//example 2
return System.out.println(super.getAnswer());

Apologies for the rubbish code. Just looking for opinions on this :unsure:
 
Last edited:
Code:
class A {
    public string Answer { get; } = "ham";
}

var a = new A();
System.out.println(a.Answer)
 
I take it you're referring to inheritance, for example:
Java:
public class A {
  private String Answer = "ham";
  public String getAnswer() {
    return Answer;
  }
}

public class B extends A {
}

// usage example
B b = new B();
System.out.println(b.getAnswer()); // ham

If however you really just wanted to access a preset variable without inheritance; then you're looking for a static variable, for example:

Java:
public class A {
  public static String Answer = "ham";
}

System.out.println(A.Answer); // ham

// ...and it work the same in a method in B
 
public class B {
  public void printAnswer() {
    System.out.println(A.Answer); // ham
  }
}
 
Last edited:
I'd probably make answer a protected rather than private variable, and just use it directly.

If B needs to access answer and it inherits from A, why make it go via the public interface? The only reason I can think of is that there might be some logic involved in getting the answer, in which case, keep the variable private and have it accessed via the get method.

Also, think about whether the value of answer can change. If not, make it const or readonly (depending on the language).
 
Value isn't changing. Get where both you guys are coming from.

B always inherits from A but the value of A.answer never changes once B is instantiated thus why I'm asking which version is better when a method is going to be used 30times, use the method or just put it in a variable. Using a variable or the getAnswer.

Not sure if I'm getting you guys correctly, or if you are hearing me :|
 
Value isn't changing. Get where both you guys are coming from.

B always inherits from A but the value of A.answer never changes once B is instantiated thus why I'm asking which version is better when a method is going to be used 30times, use the method or just put it in a variable. Using a variable or the getAnswer.

Not sure if I'm getting you guys correctly, or if you are hearing me :|

I like Droids version. If its never going to change, then a static variable makes more sense than a method call ( less overhead , minimal as it is ).
 
Value isn't changing. Get where both you guys are coming from.

B always inherits from A but the value of A.answer never changes once B is instantiated thus why I'm asking which version is better when a method is going to be used 30times, use the method or just put it in a variable. Using a variable or the getAnswer.

Not sure if I'm getting you guys correctly, or if you are hearing me :|
What's your primary concern? ... performance?
 
1. Best practice
2. Performance
If you're instantiating A once, and the result of getAnswer() is fixed; then there should be no concern about performance or memory use.

The only time that performance around accessors comes into play is when for example the result of the accessor method is dynamic (derived by computation) and/or based on an input argument; in which case memoization could be considered.
 
Preference? what's that in reference to?

Meant since there is no performance difference and you didn’t mention best practice.. it’s left to the dev on how they implement this. Either with the get method or using a variable
 
Meant since there is no performance difference and you didn’t mention best practice.. it’s left to the dev on how they implement this. Either with the get method or using a variable
Yeah... Java's limited in this respect, so choice of method vs accessible variable really hinges on what access you want to provide and/or restrict.
 
Value isn't changing. Get where both you guys are coming from.

B always inherits from A but the value of A.answer never changes once B is instantiated thus why I'm asking which version is better when a method is going to be used 30times, use the method or just put it in a variable. Using a variable or the getAnswer.

Not sure if I'm getting you guys correctly, or if you are hearing me :|

If its never changing, it should be final and static
 
Top
Sign up to the MyBroadband newsletter
X