• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yeap. You can.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic