Results 1 to 14 of 14
  1. #1
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default 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

  2. #2
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default 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?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

  4. #4
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: How many objects in this code?

    Quote Originally Posted by Norm View Post
    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?
    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?

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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; 
    	}
    }
    My answer was that it seemed to copy by reference rather than by value. I assumed that Java was only copy by value.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How many objects in this code?

    Quote Originally Posted by Wnt2bsleepin View Post
    My answer was that it seemed to copy by reference rather than by value.
    Wrong! Java copies the value of the reference. Two references that have the same value refer to the same object.

    Objects can't be copied. If the class implements cloneable, its instances can be cloned, but that's a different story.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: How many objects in this code?

    Quote Originally Posted by DarrylBurke View Post
    Wrong! Java copies the value of the reference. Two references that have the same value refer to the same object.

    Objects can't be copied. If the class implements cloneable, its instances can be cloned, but that's a different story.

    db
    So it is copying the value of the memory location (value) and assigning it to the new variable (reference). I seem to be getting myself confused between the two. Thanks for the clarification.

  8. #8
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: How many objects in this code?

    Quote Originally Posted by DarrylBurke View Post
    Wrong! Java copies the value of the reference. Two references that have the same value refer to the same object.

    Objects can't be copied. If the class implements cloneable, its instances can be cloned, but that's a different story.

    db
    So I was right in the end that there is 2 objects (being referenced by 3 instance variables in total?)

    Thanks

  9. #9
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: How many objects in this code?

    Quote Originally Posted by Wnt2bsleepin View Post
    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; 
    	}
    }
    My answer was that it seemed to copy by reference rather than by value. I assumed that Java was only copy by value.
    I figured I would also include the output from the program.

    Java Code:
    Bryan
    Thomas
    Bryan
    Derp
    Derp
    I looked into it more and this is what i seemed to have come up with. I am dealing with C++ as well, so I hope I am not confusing the two.

    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; 
        }
    }
    I still feel a little confused by it but I think that is the gist of it.

    That means that yes, there are only 2 objects.
    Last edited by Wnt2bsleepin; 01-19-2013 at 11:19 PM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How many objects in this code?

    Quote Originally Posted by Wnt2bsleepin View Post
    So it is copying the value of the memory location (value) and assigning it to the new variable (reference).
    No, it is assigning the value of one reference variable to another reference variable. In Java, you don't have to concern yourself with memory locations.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    huntack is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default 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 1: Create a reference variable a, but do not assign it to an object. If 'a' is declared as an instance variable, it is pointing to a 'null' space. (If you don't initialize it later but try to use it, you will get a NullPointerException). If you place that in a method, and try to use it later without initializing, the compiler will be upset and won't compile, saying that Person a may not have been initilaized. At this point, you have one reference variable, zero objects.

    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);
    The compiler will say: (line 1:) Reference variable a pointing to String object "Test". (line 2:) Reference variable b getting the value of Ref variable a (that is, pointing to the same object as a is!). (line 4:) modify the value of the object b is referencing to. BUT since it is String, The compiler is going to create a new String object for you and assign the value "modified" to it.

    line 6: Test Modified

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How many objects in this code?

    Quote Originally Posted by huntack View Post
    (line 4:) modify the value of the object b is referencing to. BUT since it is String, The compiler is going to create a new String object for you and assign the value "modified" to it.
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    huntack is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: How many objects in this code?

    Quote Originally Posted by DarrylBurke View Post
    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.

    db
    Thanks for clearing that up :-)

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How many objects in this code?

    Quote Originally Posted by DarrylBurke View Post
    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.
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. New to Java. Need help writing code. (Classes and Objects)
    By AustinStanley in forum New To Java
    Replies: 6
    Last Post: 11-09-2012, 11:19 AM
  2. Replies: 3
    Last Post: 05-08-2012, 08:16 PM
  3. code for moving objects at same time
    By Muhammad Assad in forum New To Java
    Replies: 13
    Last Post: 06-20-2010, 05:11 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. Java code that displaces objects
    By efsiken in forum New To Java
    Replies: 1
    Last Post: 11-25-2008, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •