Results 1 to 14 of 14
Thread: How many objects in this code?
- 01-18-2013, 09:13 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
How many objects in this code?
So lets say I made up a class called Sun, and now I want to create objects of Sun.
Sun happy = new Sun();
Sun sad = new Sun();
Sun neutral = happy();
So at first glance I thought that there was two Sun objects: happy and sun.
I think this because I used a constructor to create 2 new objects of Sun and store them onto the instance variables happy and sad.
I then create an instance variable called neutral of type Sun, which I believe copies the reference stored in happy onto itself.
But now I am starting to think there is 3 objects, because maybe Sun neutral makes a third object even though it stores the same reference as happy.
Basically I have looked at this code so much that I have really confused myself and getting mixed up with the difference between instance variables and objects.
An object is an instance of a class, and an instance variable is a reference to that object, or instance of a class in memory, i think?
So the answer is that there is two objects, being referenced by 3 instance variables?
Am i getting somewhere? lol thanks
- 01-18-2013, 09:15 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: How many objects in this code?
Ok the more I look at it the more I believe neutral is just an instance variable of type sun (meaning it is compatible to store other sun objects) so there is only 2 sun objects in the end, that we created with new sun() in the first 2 statements.
Can someone tell me if I am correct?
- 01-18-2013, 10:07 PM #3
Re: How many objects in this code?
The posted code uses happy two ways:
one as a variable
one as a method. What does the happy() method do? Where does it get a Sun object to return?Last edited by Norm; 01-18-2013 at 10:09 PM.
If you don't understand my response, don't ignore it, ask a question.
- 01-18-2013, 10:55 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: How many objects in this code?
My bad there is no happy() method, it is supposed to be the existing variable:
Sun happy = new Sun();
Sun sad = new Sun();
Sun neutral = happy;
So we are copying the reference stored in the instance variable happy, into a new variable of type sun called neutral.
So this code has 2 objects, 1 object is referenced by two instance variables happy and neutral) and the other is referenced by sad, is that correct?
- 01-18-2013, 10:59 PM #5
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 7
Re: How many objects in this code?
You could also test this yourself like I did.
Java Code:public class Test { public static void main(String[] args){ Person a = new Person("Bryan"); Person b = new Person("Thomas"); Person c = a; System.out.println(a.getName()); System.out.println(b.getName()); System.out.println(c.getName()); c.setName("Derp"); System.out.println(a.getName()); System.out.println(c.getName()); } } class Person{ String name; public Person(String name){ this.name = name; } public String getName(){ return name; } public void setName(String name){ this.name = name; } }
- 01-19-2013, 06:09 AM #6
Re: How many objects in this code?
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 01-19-2013, 07:47 PM #7
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 7
- 01-19-2013, 09:26 PM #8
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
- 01-19-2013, 11:04 PM #9
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 7
Re: How many objects in this code?
I figured I would also include the output from the program.
Java Code:Bryan Thomas Bryan Derp Derp
This is my understanding after reading this
Java apparently makes two copies when you call a method. The original reference and the reference the method is using. Since they both point to the same object, when you modify one you change the other. That would be why the original values are modified by my program below
Java Code:public class Test { public static void main(String[] args){ Person a = new Person("Bryan"); Person b = new Person("Thomas"); Person c = a; System.out.println(a.getName()); System.out.println(b.getName()); System.out.println(c.getName()); c.setName("Derp"); System.out.println(a.getName()); System.out.println(c.getName()); System.out.println("______________________"); System.out.println(b.getName()); modPerson(b); System.out.println(b.getName()); } public static void modPerson(Person person){ person.setName("Modified By person"); System.out.println(person.getName()); } } class Person{ String name; public Person(String name){ this.name = name; } public String getName(){ return name; } public void setName(String name){ this.name = name; } }
That means that yes, there are only 2 objects.Last edited by Wnt2bsleepin; 01-19-2013 at 11:19 PM.
- 01-20-2013, 02:52 AM #10
- 01-20-2013, 10:00 AM #11
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Re: How many objects in this code?
So.. in a pseudo-code like version:
Java Code:Person a; Person b = new Person("Kevin"); Person c = b;
line 2: Create reference variable b, point it to object Person which was created with String "Kevin". At this point, we have two reference variables, one object.
line 3: Create reference variable c, assign it the same value as reference variable b has. Meaning, you do not modify the Person object, you just tell java that "Copy the value of ref variable a to ref variable b, I wan them to point to the same object". At this point, we have 3 reference variables, of which 2 are the same (copies of each other), and one object.
Exception: String object. When you do the following:
Java Code:String a = "Test"; String b = a; b = "modified"; System.out.println(a + " " + b);
line 6: Test Modified
- 01-20-2013, 03:17 PM #12
Re: How many objects in this code?
Nothing's modified, and the compiler doesn't create objects. Objects exist only at runtime.
Also, all String literals in your code are created as String objects and placed in the String pool when the class is loaded. So, in line 4, b is assigned the value of the reference to the String "modified" which already exists in the String pool.
The only new object created when those 6 lines of code are run is the String "Test modified" concatenated from 3 Strings already in the pool.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 01-21-2013, 09:47 AM #13
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
- 01-21-2013, 10:25 AM #14
Re: How many objects in this code?
Also, if the variables a and b are either final fields or method-local variables (and hence their value couldn't possibly be changed by another Thread), a modern compiler just might optimize a + " " + b to another String in the pool, and no object at all would be then created during the execution of those lines of code.
Overall, this is not something you need to worry your head about, though. Unless you have a large code that is creating and discarding a very large number of objects -- String or otherwise -- and frequent garbage collection impacts performance. And then, optimization should be carried out only after analyzing and profiling the running code -- not by guesswork.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
New to Java. Need help writing code. (Classes and Objects)
By AustinStanley in forum New To JavaReplies: 6Last Post: 11-09-2012, 11:19 AM -
cant understand objects life in this particular code
By broli in forum New To JavaReplies: 3Last Post: 05-08-2012, 08:16 PM -
code for moving objects at same time
By Muhammad Assad in forum New To JavaReplies: 13Last Post: 06-20-2010, 05:11 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
Java code that displaces objects
By efsiken in forum New To JavaReplies: 1Last Post: 11-25-2008, 10:15 AM
Bookmarks