Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying out this program to clarify doubts regarding getter and setter methods but I got an unexpected error "StackOverflowError" in line1 mentioned in below code:
Java
public class GetterSetter {
    private int a=10;
   GetterSetter gs=new GetterSetter();//line 1
   static GetterSetter gsr=new GetterSetter();//line 2
    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }
    public void setB(int b)
    {
       return; 
    }
    public int getB()
    {
    return 1;
    }
    public int check()
    {return a;}
}
class Check
{   Check c=new Check();//line 3
    public static void main(String[] args) {
        GetterSetter gs=new GetterSetter();
        gs.setA(50);
        System.out.println(gs.getA()+"\n"+gs.check());
    }
}


Could anyone explain the problem here? Am confused.

What I have tried:

I tried line 2 and 3 but no error in line 2 and also in line3
Posted
Updated 17-Aug-18 5:15am
v2

1 solution

Because, with line 1, every time you instantiate a GetterSetter, you instantiate an additional GetterSetter, which (because of line 1) requires you to instantiates an additional GetterSetter, which instantiates an additional GetterSetter, and so on, and so on...ad infinitum.

The "static" lines are different, because you only instantiate one GetterSetter that is shared between all instances.

For more on the differences between static and non-static fields, see "Static and Non-Static Fields" at the following link:

http://tutorials.jenkov.com/java/fields.html
 
Share this answer
 
v2
Comments
@k5hu 17-Aug-18 14:03pm    
Sir, but why there is no error in line 3?
Eric Lynch 17-Aug-18 16:32pm    
Honestly, for line 3, I'm am surprised you do not see the same problem. If it is truly instantiated, and utilized, I can not see why "check" would not also cause an issue. To answer questions, about the difference, I would need to see both the instantiation and utilization of both classes.

Since I cannot, I suspect maybe you are assuming a similarity in instantiation or utlization that does not exist. If you are describing everything accurately, and completely, the difference in behavior between two nearly identical classes makes zero sense. The only difference in declaration is a difference in class protection ("public" vs default). This would not make sense as an explanation for the difference in behavior...absent an accompanying difference in instantiation / utilization.
@k5hu 18-Aug-18 1:48am    
ouptut:
java.lang.StackOverflowError
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
at oca.GetterSetter.<init>(GetterSetter.java:16)
.
.
.
only line 16 which in here is line 1 is mentioned

but in this :
class Sample {

Sample s = new Sample();//line1
static Sample sa = new Sample();

void check(Sample s) {
System.out.println(this.s == s);
}


}

public class Other
{ Other o=new Other();//line 2
static Other ot=new Other();
public static void main(String[] args) {
new Sample().check(new Sample().s);
}
}
output:
same error in line 2

and in this:
public class Sample {

Sample s = new Sample();//line1
static Sample sa = new Sample();

void check(Sample s) {
System.out.println(this.s == s);
}

public static void main(String[] args) {
new Sample().check(new Sample().s);
}
}

class Other
{ Other o=new Other();//line2
static Other ot=new Other();

}
but here ouput :
same error in line 1

by observing, I found that its happening only with public classes but I can't figure out the exact reason. Could you please help me understand this if possible?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900