This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!