• 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

Object state

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic