Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can 2 objects of the same class acess private data of each other?

 
Seema Ahuja
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys another question:

i want to know how an object of a class can access the variable of another object of the same class?
i get stack overflow error when i compile the following program:



Also if i delare both the objects in the main...there is no way i can access the 2nd object with the refrence of the 1st.
what i mean is:


can anyone pl tell me is there any way we can acess the object of the same class with out declaring it as static within that class.
 
jeancarlo sott
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm getting the error stackoverflowerror in this case too:



The instantiation cannot be on the class scope or in an contructor.

Hope someone can clarify bether.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Seema,

Both code fragments can compile and both raises StackOverFlowError just because when you create and instance the JVM automatically creates another instance to assign to inc member and so on. The program enters in a infinite recursive calls.

I think you are trying to do something like a list where a node points to another node and so on.

So, I belive the code below would be suitable for you.

 
Abhinav Gogna
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just put that as an Instance variable instead of defining in the initializer block.

For eg:

class abc{
private int 1; // ----> is accessible by all the methods within the class only.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jeancarlo, please click on the My Profile link above and add a space between your first and last name.

Thanks

Mark
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeap. You can.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic