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

    Default how to get to a method of an inner class

    Imagine I have a class called Outer that contains a Inner class with a method.
    Now there is a second class which wants to call that method:

    Java Code:
    public class Outer {
        public Outer()  {
            Inner inner = new Inner();
            inner.createdInnerMethod();
            System.out.println("Outer constructor");
        }
    
    	class Inner {
    		public void createdInnerMethod() {
                        System.out.println("inner method");
    		}
    	}
    }
    
    class Second { // how to get here into createdInnerMethod()
    
            public Second()  {
                Outer outer = new Outer();
                System.out.println("Second constructor");
    
            }
    
    	public static void main(String arg[]) {
    		Second second  =  new Second();
    
    	}
    }
    Last edited by willemjav; 02-02-2014 at 10:48 PM.

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

    Default Re: how to get to a method of an inner class

    The idea is that the Inner class should manage data from the Outer class.
    That same data should be made available at the Second class!
    The next model will not work because the Inner class exists twice
    and should only live once! So what to do?

    Java Code:
    public class Outer {
      
        public Outer(int sum)  {
            sum++;
            Inner inner = new Inner(sum);
            inner.createdInnerMethod("first: ", sum);
            System.out.println("Outer constructor");
        }
    
    	class Inner {
                int sum;
                Inner(int sum)  {
                    this.sum++;
                }
    		public void createdInnerMethod(String s, int sum) {
                        sum++;
                        System.out.println(s + sum);
    
    		}
    	}
    }
    
    class Second { // how to get here into createdInnerMethod()
            
            public Second(int sum)  {
                sum++;
                Outer outer = new Outer(sum);
                Outer.Inner inner = outer.new Inner(sum);
                inner.createdInnerMethod("second: ", sum);
                System.out.println("Second constructor");
    
            }
    
    	public static void main(String arg[]) {
    		Second second  =  new Second(1);
    
    	}
    }
    I need to look again into the concept of static inner classes, right?
    A concept I did not understand fully!
    Last edited by willemjav; 02-02-2014 at 11:44 PM.

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

    Default Re: how to get to a method of an inner class

    This is confusing. First, why do you pass sum in the constructor? You are not doing anything with it.

    Java Code:
    class Inner {  
           int sum;
          Inner(int sum)  {
               this.sum++;
          }
          public void createdInnerMethod(String s, int sum) {
             sum++;
             System.out.println(s + sum);
         }
    }
    I am not certain what your problem is. You are accessing the inner class' method just fine. The primary differences between an inner class and a static inner class are:

    1. A static inner class may not access state information from the enclosing class.
    2. An static inner class may be constructed without requiring an instance of the enclosing class (which is why #1 is required). No instance, no state. Multiple instances, then which state do you use. Similar analogy disallows trying to access instance fields from static methods.

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

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

    Default Re: how to get to a method of an inner class

    Jim, you are right, the example is confussing! Here is the situation I am at.

    I am at the last phase of the first part of my text writer application: the basic GUI (file, edit and font menu interacting with a tool bar). Next I will get into the text canvas (HAVE LOTS OF FUN THAN).

    Everting works but I need to make five variables avaible (it was discussed before and kjkrum said :"This is a good case for the MVC design pattern").

    Here is the list:
    -currentFiles: an dynamic arraylist of the open text files (will permit more than one file open)

    -currentFont: text font family, String

    -currentSyle: an object of an inner class (see code example)

    -currentSize: an integer value

    -currentColor: of Color

    You see the text style issue is a little more complicated because there is
    plain text versus:
    1) bold
    2) italic
    3) underlined
    4) lined (I do not have yet the correct term here for text that you write as "misstaken" with a line going through)

    So I created an object with four sepparet booleans (see code example), right!

    This inner class gets its info (setting the four booleans) at the class MenuFontMenu2, but the data (togther with currentSize and currentColor) should be made availabe yet in an other class: MenuFontMenu where the missing variable currentFont is created by the GUI.

    So here you have my three class model of:
    1) inner class of
    2) MenuFontMenu2 and the third class
    3) MenuFontMenu where the data should be availble

    Yet an other (fourth) class is in the make (kjkrum mvc model) that should hold all data (the file names and the four font issues).
    This class will be a independent, single class that implements the PropertyChangeListener and changeSupport we discussed so well before!

    BTW as you will notice I also added the currentSize and color to the class --maybe not a good idea-- since they also should be availble at the MenuFontmenu class where only the font family is set (currentFont string).

    And stile to design the fourth class of the MVC model!

    Java Code:
    // this class holds all the current font style info
        class CurrentFontStyle {
            boolean bold, italic, underlined, lined;
            int size;
            Color color;
    
            public boolean isBold() {
                return bold;
            }
            public void setBold(boolean b) {
                bold= b;
            }
    
            public boolean isItalic() {
                return italic;
            }
            public void setItalic(boolean b) {
                italic= b;
            }
    
            public boolean isUnderlined() {
                return underlined;
            }
            public void setUnderlined(boolean b) {
                underlined = b;
            }
    
            public boolean isLined() {
                return lined;
            }
            public void setLined(boolean b) {
                lined= b;
            }
    
            public int getSize() {
                return size;
            }
            public void setSize(int s) {
                size= s;
            }
    
            public Color getColor() {
                return color;
            }
            public void setColor(Color c) {
                color= c;
            }
    
    
        }

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

    Default Re: how to get to a method of an inner class

    I again noticed, jim, that I should change method names like setColor and setSize for
    not running in the same set of problems!

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

    Default Re: how to get to a method of an inner class

    How silly of mine!
    MenuFontMenu2 fontMenu2 = new MenuFontMenu2(textcanvas, changeFontSupport);
    fontMenu2.getCurrentFontStyle().getFontColor(); // I changed getColor() into getFontColor() etc.
    Makes the inner class fully available!
    I think by just calling it a INNER CLASS my brain got paralyzed: the Java Inner-class-trauma hahahaha

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

    Default Re: how to get to a method of an inner class

    I am a teacher as well (into another area) and I use say:
    Form the mistakes of our students we learn the most!

Similar Threads

  1. Replies: 5
    Last Post: 07-12-2012, 08:44 PM
  2. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  3. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  4. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  5. Replies: 5
    Last Post: 01-26-2011, 06:38 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
  •