• Post Reply Bookmark Topic Watch Topic
  • New Topic

access the instance of a class in another class?  RSS feed

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I access the instance x of class X, which is made in class c1 in another class c3?






now I want access the instance that is made in Class c1 in another class c2:



How can I do that? With reflection or something else???

[ September 09, 2008: Message edited by: nimo frey ]
[ September 09, 2008: Message edited by: nimo frey ]
 
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nimo frey:



How can I do that? With reflection or something else???



When you say It 'does not work' , What error the compiler raise ?
 
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One solution could be to make an instance of class c1 in class c2, and then call as you are started.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this:



And I get this error, when I try to get the instance:




But I do not need a new Instance, I need exactly the instance created by the other class.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I have do the class-instance "static" ??

I have no clue?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to look at the Java Singleton Design Pattern

You can google for more samples.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nimo frey:
Maybe I have do the class-instance "static" ??

I have no clue?


Seems like that will work If you make the getX() method static or create an object of class 'c1' and then call getX() method on that object !
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, now it works:







I use this in a Web-Application, I hope it does not matter in anyway. It seems to work.

thanks!!!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nimo frey:
I use this in a Web-Application, I hope it does not matter in anyway. It seems to work.


Every instance of your c1 class will now share the same instance of X. If you change your x variable in one c1 instance, it will be changed in all c1 instances. If that is what you want, then your code is fine.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nimo frey:

I don't think that's what you mean. I think you mean the class equivelent of "this.x = x": c1.x = x;
[ September 09, 2008: Message edited by: Rob Prime ]
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes, that is what I want:-) The instance is available in my Application-Scope of my Websystem and needs to be the same for the whole application.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!