This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!