Reputation: 125
A ton of questions have been asked on how to create getter and setter methods in java. But i have yet to see one that actually tells me how to use it.
Say i have Private int i = 1;
in class A and i want to access it in class B.
I would first create a get method in class A called getIntI();
which would return the value of i.
Then in class B if i wanted to create an if statement that would need the value of i how would I get int i's value. The following is my try and calling the get method which does not work.
if(getIntI == 1)
{System.out.print.ln("int i is one");}
It is probably a really stupid question but i cant find an answer for it elsewhere.
Upvotes: 2
Views: 3628
Reputation: 271135
First, if you want to access one of A
's non-static methods (in this case, getIntI
), you need an instance of A
, or you can just declare it static.
Secondly, A method call needs a parameter list, even an empty one is needed. getIntI
does not need any parameters, so you should add ()
at the end.
Now, you can get an instance of A
somewhere and call it aObj
. Andd then you can use it in the if statement:
if (aObj.getIntI == 1)
And remember to add ()
!
if (aObj.getIntI() == 1)
Alternatively, you can declare i
in A
as static. There are two main differences between a static and a non-static variable.
You don't need an instance of the declaring class to access the static variable.
Unlike non-static variables, there is only one static variable. If you have a non-static variable i
, you can create lots of instances of A
and each instance will have its own i
Now let's see this in action, declare i
as static:
public class A {
private static int i = 1;
public static int getIntI () { return i; }
}
Note how both i
and getIntI
are declared static.
Then you can use in a if statement like this:
if (A.getIntI() == 1)
Note how I use the class name A
to access the method, not an instance of A
.
Upvotes: 0
Reputation: 2034
In class A:
public int getIntI(){
return i;
}
Note: Now since your variable is single character named (just I
), getter method is named getIntI
since the name getI
makes lesser sense. But generally, getter methods are something like get+VariableName
and do not involve mentioning type. For example if I had a variable called int count
, my method would be named getCount
instead of getIntCount
. Thats the general convention.
Also, naming variables in single char formats (like x
, y
etc) is discouraged because it may create confusion and management difficulty in complex programs. Though in very small programs they are fine.
Moving back to topic, if you want to access method getIntI()
in class B, you will either have to inherit class A or create an object of class A reference to its method.
For class B:
Creating object
A obj = new A();
if(obj.getIntI() == 1)
// Do stuff
Inheriting class A:
public class B extends A{
... // Your stuff
if(getIntI() == 1)
// Do stuff
... // Your stuff
}
Of course there are other ways but these are simpler ones.
Upvotes: 3
Reputation: 22
The problem is that you need to create a object derived from class A before you can access its variables/methods using
A a = new A();
where "a" is the name of the object. Then you can access the getter method by calling a.getIntI
.
You can also declare the int variable as static so that you wouldn't have to instantiate any objects. An example of class A with the static variable and getter method would be:
public class A {
private static int i = 1;
public static int getIntI() {
return i;
}
}
With this, you can call the getter method with A.getIntI()
.
Upvotes: 0
Reputation: 4207
if class B extends class A
then do only this changes,
if(getIntI() == 1)
If above inheritance was not there
then do this,
if(new A().getIntI() == 1)
Upvotes: 0