Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
  1. #21
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Struggling with inner classes

    What you think of that page

    Java - Inner Classes

    I liked it has a good explanation I believe.

  2. #22
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Struggling with inner classes

    so when printing "this": willemlearningjava.TopLevel2$Foobar@33dff3a2
    so this refers to the inner class Foobar

    Java Code:
    public void printv() {
             TestStaticClasses r = new TestStaticClasses();
             System.out.println("no1 " + r.v);
             System.out.println("no2 " + s);
             System.out.println("no3 " + this.s + " print actual object " + this);
          }

  3. #23
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Struggling with inner classes

    Quote Originally Posted by willemjav
    so when printing "this": willemlearningjava.TopLevel2$Foobar@33dff3a2
    so this refers to the inner class Foobar

    Correct!
    Last edited by jim829; 02-22-2013 at 08:26 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #24
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Struggling with inner classes

    Quote Originally Posted by willemjav View Post
    so when printing "this": willemlearningjava.TopLevel2$Foobar@33dff3a2
    so this refers to the inner class Foobar
    Nope.

    this refers to the instance of the inner class Foobar within which that code is executing,.

    There's a subtle but important difference between a class and an instance of a class.

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

  5. #25
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Struggling with inner classes

    Sigh... I inferred he was talking about instances. But of course, you are correct.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #26
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Struggling with inner classes

    Imagen there is a public OuterClass and a private InnerClass.
    would a) and b) by the right thing to do?

    OuterClass mo = new OuterClass();
    a) OuterClass.InnerClass inner = new OuterClass().new InnerClass();
    b) OuterClass.InnerClass inner = mo.new InnerClass();

  7. #27
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Struggling with inner classes

    Any idea how I get the lines 11, 12 to work?

    Java Code:
    public class A {    // outer class
        private String x = "hello outer x";
        public static void main (String[ ] args) {
        A a = new A();
        A.B b = a.new B();
        a.outerf("outer");
        b.innerf("outer");
        }
        void outerf(String cl) {
            System.out.println(" from " + cl + " print " + x);
         // System.out.println(" from " + cl + " print " + b.y); WON'T WORK
         // b.innerf("outer");                                   WON'T WORK
        }
    
        private class B {   // private inner class
            public String y = "hello inner y";
            void innerf(String cl) {
                System.out.println(" from " + cl + " print " + x);
                System.out.println(" from " + cl + " print " + y);
                outerf("inner");
            }
           
    
    
        }
    
    }
    Last edited by willemjav; 02-25-2013 at 03:35 PM.

  8. #28
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Struggling with inner classes

    You have several problems.

    The first is that A.B b and A a are declared as local variables. So their scope is limited to "main." You need to move the A.B b declaration outside of main. Second, assuming you get it to work, you have created a calling loop where outerf calls innerf which calls outerf.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #29
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Struggling with inner classes

    You'r right jim, I should have know, but to many stuff to think of!
    This one works and makes more sense....
    The idea is to jump from inner to outer and vis versa

    Java Code:
    public class A {    // outer class
        private String x = "hello outer x";
        private static A a;
        private static A.B b;
    
        public static void main (String[ ] args) {
        a = new A();
        b = a.new B();
        a.outerf("outer");
        b.innerf("outer");
        b.callInnerOuterf("outer/inner");
        }
        void outerf(String cl) {
          System.out.println(" from " + cl + " print " + x);
          System.out.println(" from " + cl + " print " + b.y);                                         
        }
    
        private class B {   // private inner class
            public String y = "hello inner y";
            void innerf(String cl) {
                System.out.println(" from " + cl + " print " + x);
                System.out.println(" from " + cl + " print " + y);
               
            }
            void callInnerOuterf(String cl)  {
                outerf(cl);
            }
    
    
        }
    
    }
    Last edited by willemjav; 02-25-2013 at 05:13 PM.

  10. #30
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Struggling with inner classes

    A class instantiation with inner class instantiations looks like a blob with smaller blobs attached to it; it looks like a raspberry or even a solar system; the outer class instantiation is the star or sun while the planets are the small blobs circling around the outer blob etc. etc. Maybe you appreciate the following example:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    class SolSysData {
    	public static String star = "Sun";
    	public static String[] planets = { "Mercury", "Venus", "Earth", "Mars",
    			"Ceres", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto",
    			"Haumea", "Makemake", "Eris" };
    
    	public static long[] distance = { 57909175L, 108208930L, 149597890L,
    			227936640L, 413781779L, 778412020L, 1426725400L, 2870972200L,
    			4498252900L, 5906380000L, 6482824563L, 6850236980L, 10123004980L };
    
    	public static String[][] moons = {
    			{},
    
    			{},
    
    			{ "Moon" },
    
    			{ "Phobos", "Deimos" },
    
    			{},
    
    			{ "Io", "Europa", "Ganymede", "Callisto", "Amalthea", "Himalia",
    					"Elara", "Pasiphae", "Sinope", "Lysithea", "Carme",
    					"Ananke", "Leda", "Thebe", "Adrastea", "Metis",
    					"Callirrhoe", "Themisto", "Megaclite", "Taygete",
    					"Chaldene", "Harpalyke", "Kalyke", "Iocaste", "Erinome",
    					"Isonoe", "Praxidike", "Autonoe", "Thyone", "Hermippe",
    					"Aitne", "Eurydome", "Euanthe", "Euporie", "Orthosie",
    					"Sponde", "Kale", "Pasithee", "Hegemone", "Mneme", "Aoede",
    					"Thelxinoe", "Arche", "Kallichore", "Helike", "Carpo",
    					"Eukelade", "Cyllene", "Kore", "Herse", "S/2003 J2",
    					"S/2003 J3", "S/2003 J4", "S/2003 J5", "S/2003 J9",
    					"S/2003 J10", "S/2003 J12", "S/2003 J15", "S/2003 J16",
    					"S/2003 J18", "S/2003 J19", "S/2003 J23" },
    
    			{ "Mimas", "Enceladus", "Tethys", "Dione", "Rhea", "Titan",
    					"Hyperion", "Iapetus", "Erriapus", "Phoebe", "Janus",
    					"Epimetheus", "Helene", "Telesto", "Calypso", "Kiviuq",
    					"Atlas", "Prometheus", "Pandora", "Pan", "Ymir", "Paaliaq",
    					"Tarvos", "Ijiraq", "Suttungr", "Mundilfari", "Albiorix",
    					"Skathi", "Siarnaq", "Thrymr", "Narvi", "Methone",
    					"Pallene", "Polydeuces", "Daphnis", "Aegir", "Bebhionn",
    					"Bergelmir", "Bestla", "Farbauti", "Fenrir", "Fornjot",
    					"Hati", "Hyrrokkin", "Kari", "Loge", "Skoll", "Surtur",
    					"Greip", "Jarnsaxa", "Tarqeq", "Anthe", "Aegaeon",
    					"S/2004 S7", "S/2004 S12", "S/2004 S13", "S/2004 S17",
    					"S/2006 S1", "S/2006 S3", "S/2007 S2", "S/2007 S3",
    					"S/2009 S1" },
    
    			{ "Cordelia", "Ophelia", "Bianca", "Cressida", "Desdemona",
    					"Juliet", "Portia", "Rosalind", "Mab", "Belinda",
    					"Perdita", "Puck", "Cupid", "Miranda", "Francisco",
    					"Ariel", "Umbriel", "Titania", "Oberon", "Caliban",
    					"Stephano", "Trinculo", "Sycorax", "Margaret", "Prospero",
    					"Setebos", "Ferdinand" },
    
    			{ "Triton", "Nereid", "Naiad", "Thalassa", "Despina", "Galatea",
    					"Larissa", "Proteus", "Halimede", "Psamathe", "Sao",
    					"Laomedeia", "Neso" },
    
    			{ "Charon", "Hydra", "Nix" },
    
    			{ "Hi'aka", "Namaka" },
    
    			{},
    
    			{ "Dysnomia" } };
    }
    
    public class SolarSystem {
    
    	private String name;
    	private List<Planet> planets = new ArrayList<Planet>();
    
    	public SolarSystem(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public List<Planet> getPlanets() {
    		return planets;
    	}
    
    	public class Planet {
    
    		private String name;
    		private long distance;
    
    		private List<Moon> moons = new ArrayList<Moon>();
    
    		public Planet(String name, long distance) {
    
    			this.name = name;
    			this.distance = distance;
    
    			planets.add(this);
    		}
    
    		public String getName() {
    			return name;
    		}
    
    		public long getDistance() {
    			return distance;
    		}
    
    		public List<Moon> getMoons() {
    			return moons;
    		}
    
    		public class Moon {
    
    			private String name;
    
    			public Moon(String name) {
    				this.name = name;
    				moons.add(this);
    			}
    
    			public String getName() {
    				return name;
    			}
    
    			public String toString() {
    				return name + " (" + Planet.this + ")";
    			}
    		}
    
    		public String toString() {
    			return name + "[" + distance + "] (" + SolarSystem.this + ")";
    		}
    	}
    
    	public String toString() {
    		return name;
    	}
    
    	public static void main(String[] args) {
    
    		SolarSystem star = new SolarSystem(SolSysData.star);
    
    		for (int i = 0; i < SolSysData.planets.length; i++) {
    			SolarSystem.Planet planet = star.new Planet(SolSysData.planets[i],
    					SolSysData.distance[i]);
    
    			for (int j = 0; j < SolSysData.moons[i].length; j++)
    				planet.new Moon(SolSysData.moons[i][j]);
    		}
    
    		System.out.println(star + ":");
    
    		for (SolarSystem.Planet planet : star.getPlanets()) {
    			System.out.println("\t" + planet + ":");
    
    			for (SolarSystem.Planet.Moon moon : planet.getMoons())
    				System.out.println("\t\t" + moon);
    			System.out.println();
    		}
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #31
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Struggling with inner classes

    Wow, jos, what can I say, you have locked my out for the next days to digest
    you creation of a universe (the universe as a comp program......)
    thanks, I'll start working on it

  12. #32
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Struggling with inner classes

    Quote Originally Posted by willemjav View Post
    Wow, jos, what can I say, you have locked my out for the next days to digest
    you creation of a universe (the universe as a comp program......)
    thanks, I'll start working on it
    It's no universe; it's a single solar system with one star (the outer class), the planets (the inner class of the the star class and the moons (the inner class of the planet class). Non-static nested classes have a 'secret' pointer to their enclosing class (see Planet.this and SolarSystem.this). This classs (and its inner classes) can only handle a single star and its planets (and their moons); 'twin stars' have to be modeled differently ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #33
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Struggling with inner classes

    Quote Originally Posted by JosAH View Post
    It's no [s]universe[/s]moon;
    It's a space station!

    ETA: damn...which one is strikethrough?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #34
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Struggling with inner classes

    Quote Originally Posted by Tolls View Post
    It's a space station!

    ETA: damn...which one is strikethrough?
    No, it's <drum roll/> pigs in space!

    kindest regards,

    Jos (oink ;8)
    cenosillicaphobia: the fear for an empty beer glass

  15. #35
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Struggling with inner classes

    Come, put some effort into it!

    Pigs...In....SPAAAAAAACE!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Struggling with inner classes

    Quote Originally Posted by Tolls View Post
    Come, put some effort into it!

    Pigs...In....SPAAAAAAACE!
    Shh! They might hear us and we wouldn't want that, would we?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Struggling need help
    By acbar in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 05:00 AM
  2. Struggling with overriding abstract classes
    By Stubkan in forum New To Java
    Replies: 3
    Last Post: 03-12-2012, 10:07 AM
  3. Struggling to import JAR
    By Mr ChriZ in forum Android
    Replies: 0
    Last Post: 01-12-2012, 10:53 AM
  4. Struggling with this code
    By Shimless12 in forum New To Java
    Replies: 5
    Last Post: 07-14-2011, 06:06 PM
  5. Struggling with AlphaComposite .. please help me
    By jeshmal4u in forum Java 2D
    Replies: 3
    Last Post: 03-08-2010, 05:11 AM

Tags for this Thread

Posting Permissions

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