Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jim829

Thread: Difference between instantiating a class in a method and that in global scope?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Difference between instantiating a class in a method and that in global scope?

    Java Code:
    public class Outer {
    	
    	private class Inner {}
    	
    	
    	Inner inner = new Inner();
    	
    	public static void main(String [] args) {
    		Outer outer = new Outer();
    		
    		Outer outerOne = new Outer();
    		
    		System.out.println(outer.inner.equals(outerOne.inner));
    	}
    
    }
    In the given code, two copies of inner are there in memory - one for outer, and one for outerOne. The last print statement just confirms that.

    But how many copies of inner will be created in memory in the following program?

    Java Code:
    public class Outer {
    	
    	private class Inner {}
    
    	
    	public static void main(String [] args) {
    		Outer outer = new Outer();
    
                    Inner inner = outer.new Inner();
    		
    		Outer outerOne = new Outer();
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Difference between instantiating a class in a method and that in global scope?

    If your first test was sufficient to determine the answer, why wouldn't it work for the second example? If you don't want to dwell
    on the logic, then just print out the references.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Difference between instantiating a class in a method and that in global scope?

    If your first test was sufficient to determine the answer, why wouldn't it work for the second example?
    Because I can't access inner like outer.inner or outerOne.inner, as it gives 'inner can not be resolved or is not a field'. I could print it using System.out.print(inner); but then how would I know the object being printed is that of outer or that of outerOne, assuming that both outer and outerOne have their own copies of inner.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Difference between instantiating a class in a method and that in global scope?

    My assumption is that if they have their own copies, the references would be different. If they shared a copy,
    the references would be the same. That is essentially what you checked when you compared their String representations.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Difference between instantiating a class in a method and that in global scope?

    My assumption is that if they have their own copies, the references would be different. If they shared a copy,
    the references would be the same. That is essentially what you checked when you compared their String representations.
    Yes, but I was saying that when the object named inner was created in the global scope (i.e. outside main()), I was able to use outer.inner as well as outerOne.inner in my code without the compiler complaining. That tells us that both outer and outerOne have an object named inner.

    System.out.println(outer.inner.equals(outerOne.inn er)) printed false, which means they have their own copies of inner.

    So it is established that when an inner class is instantiated in the global scope (i.e. outside any method), like Inner inner = new Inner();, each of the instances of the enclosing class gets its own copy of the inner class instance created.

    Now when the inner class instance is created inside the main() method, it has to be created using an instance of enclosing class and dot notation, like so

    Inner inner = outer.new Inner;//where outer is an instance of enclosing class

    The question here is that in this case, does outer, as well as outerOne, get a copy of the newly created object named inner? Or does only outer get a copy of inner and outerOne doesn't get anything (i.e. outerOne simply does not have an object of the inner class)?

    The other question is that when I try to access inner like outer.inner, or outerOne.inner, I get compiler error "inner can not be resolved or is not a field". Why is that?

    Java Code:
    public class Outer {
         
        private class Inner {}
     
         
        public static void main(String [] args) {
            Outer outer = new Outer();
     
            Inner inner = outer.new Inner();
             
            Outer outerOne = new Outer();
    
            System.out.print(outer.inner);//COMPILER ERROR: inner can not be resolved or is not a field.
    
            System.out.print(outerOne.inner);//COMPILER ERROR:  inner can not be resolved or is not a field.
    
        }
     
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Difference between instantiating a class in a method and that in global scope?

    In your first example, inner is an instance field of class outer. So you need to qualify its access with an instance of outer (just like any other
    instance field, accessed from a static context).

    In your second example, all the class variables are local to the main method. So they can't be qualified with a reference since they don't exist
    as instance fields.

    Regards,
    Jim
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Difference between instantiating a class in a method and that in global scope?

    I should have known this but I didn't just think like this. When I am trying to understand a little advanced concept, it all just entangles my mind and my mind stops visiting the basics.
    Last edited by Zarah; 05-17-2016 at 12:15 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Difference between instantiating a class in a method and that in global scope?

    For starters you have the terms incorrect.
    In the original code (which has 'inner' member variables) that is not global scope.
    Those 'inner' variables are member data and if they were "global" (there is not global scope, so I'm guessing a bit about the closest thing to it) then they would be static at best.

    Just remember, though...there is no "global" in Java. Everything is associated with a class in one way or another, either as a static thing or as member data for an instance of the class.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Instantiating an array with object of another class
    By AMCNovember in forum New To Java
    Replies: 11
    Last Post: 02-18-2012, 05:29 PM
  2. Replies: 1
    Last Post: 10-06-2011, 02:26 PM
  3. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  4. Replies: 0
    Last Post: 03-31-2008, 10:08 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:47 PM

Posting Permissions

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