Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Object state  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two classes ..let us consider class A and Class B.
I have created an instance of Class A that is OBJ1 and inside class A have declared few instance variables and methods.
The Object state has been changed with lot of method calls to change the instance variables. At this point i am creating an instance of Class B OBJ2 .

With a minimum number of steps have to have the OBJ2 state should be the same as OBJ1 state? How would i acheaive this?

Please let me know ...thanks
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to have obj2 to be of the same state as that of obj1 then do you mean that obj2 as well is an instance of Class A. If so why is it that you are having Class B ?
 
Rajan Venkat
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No , OBJ2 is the instance of Class B, there is no connection between OBJ2 and Class A. But i want to acheive the OBJ1 state in OBJ2 with minimum no of steps .

pleaze let me know your thoughts,
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it.

OBJ1 is an instance of A
OBJ2 is an instance of B

there is no connection between OBJ2 and Class A.



Then how can a B have the same state as an A unless there is some relationship between the classes. Anyway, the easiest way for an A to know the state of B is to give it the B as a parameter.

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do a cloning of obj1 but it will return you Object type and from there you can only take back object of ClassA type by downcasting it to type ClassA.
doncasting to ClassB will through a ClassCasteException at runtime.
i dont see any way,
if you find please let me know also
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!