2

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.

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
Ricky
  • 125
  • 2
  • 9
  • 1
    Try with `if(getIntI() == 1)` instead. You need to have parentheses in method calls in Java. – Mick Mnemonic Nov 29 '15 at 00:14
  • Your question is not specific to get methods. Rather your question is how to invoke **any method in java**. For this example, put parenthesis at the end of the method name, like `getIntI()`. – Andy Guibert Nov 29 '15 at 00:14

4 Answers4

3

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.

Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33
  • why is it sometimes when I use the get method it only works when I use private static int i. when I try to change it so its not static I get a null value when the get method is called. – Ricky Nov 29 '15 at 01:47
  • Can you show the code where it does that? Also this link might help: http://stackoverflow.com/questions/17242649/can-non-static-methods-modify-static-variables – Jaskaranbir Singh Nov 29 '15 at 01:56
  • That reflects something wrong with your design; your variables should not be static and the proper fix is something else. – Louis Wasserman Nov 29 '15 at 02:25
  • [link](http://stackoverflow.com/questions/33979003/java-using-getter-and-setter-methods-and-returning-0) here is my question regarding my problem. Any help is much appreciated! – Ricky Nov 29 '15 at 03:26
0

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)
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
0

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().

Stinlang
  • 22
  • 5
0

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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313