Difference between these two class declaration

Discussion in 'Java' started by Rach, Jun 9, 2004.

  1. Rach

    Rach Guest

    Hi,

    What's the difference between these two A.java files? Which is better? Both
    allows me to do access methodB1():

    ///------main.java-----///
    A a = new A();
    a.b.methodB1();
    ///--------------------///

    ///----------A.java----------///
    public class A {
    public B b;
    A() { b = new B(); }

    public class B {
    B() {..}
    public void methodB1() {..}
    }
    }
    ///--------------------------///


    ///----alternative A.java-----///
    public class A {
    public B b;
    A() { b = new B(); }
    }

    class B {
    B() {..}
    public void methodB1() {..}
    }
    ///---------------------------///
     
    Rach, Jun 9, 2004
    #1
    1. Advertising

  2. Rach

    VisionSet Guest

    "Rach" <> wrote in message
    news:ca6ktm$4du$...
    > Hi,
    >
    > What's the difference between these two A.java files? Which is better?

    Both
    > allows me to do access methodB1():
    >
    > ///------main.java-----///
    > A a = new A();
    > a.b.methodB1();
    > ///--------------------///
    >
    > ///----------A.java----------///
    > public class A {
    > public B b;
    > A() { b = new B(); }
    >
    > public class B {
    > B() {..}
    > public void methodB1() {..}
    > }
    > }
    > ///--------------------------///
    >
    >
    > ///----alternative A.java-----///
    > public class A {
    > public B b;
    > A() { b = new B(); }
    > }
    >
    > class B {
    > B() {..}
    > public void methodB1() {..}
    > }


    They are both bad, attributes should be private.
    That aside inner classes are useful when the inner class is closely related
    to its parent, the parent is usually solely responsible for its
    instantiation. If it is made available outside of the class it is typically
    as an interface type. It should be obvious if innerclass is appropriate,
    otherwise make it a top level class.

    --
    Mike W
     
    VisionSet, Jun 9, 2004
    #2
    1. Advertising

  3. Rach

    Andy Fish Guest

    "Rach" <> wrote in message
    news:ca6ktm$4du$...
    > Hi,
    >
    > What's the difference between these two A.java files? Which is better?

    Both
    > allows me to do access methodB1():
    >


    I would say it's mainly a question of style and should be looked at on a
    case-by-case basis. Some people almost never use inner clases and would
    always use a package to group related classses; some people use them quite
    heavily and have less packages. In terms of functionality and access control
    there is not much to choose between them.

    personally, I would tend to use an inner class if it is a very simple one
    and has very little behaviour, typically if it is a type that is only used
    as a parameter or return value to the specific outer class. a good example
    is when you want a method to return two values - I would make an inner class
    encapsulating those two values and return that. Another example is if you
    have an exception which will only be thrown by a specific class.

    two important caveats regarding your example though

    1. in the second example. make sure class B is in a separate file to class
    A - some compilers will allow a class in a file of a different name but most
    won't

    2. in the first example class B should almost certainly be marked static
    (never use non-static inner classes unless you know what they are and why
    you want to use them).
     
    Andy Fish, Jun 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guybrush Threepwood
    Replies:
    7
    Views:
    544
    Nick Hounsome
    Feb 26, 2004
  2. JohnJSal
    Replies:
    13
    Views:
    451
    John Salerno
    Nov 4, 2006
  3. oo@@oo
    Replies:
    1
    Views:
    302
    Hallvard B Furuseth
    Aug 4, 2006
  4. Manoj
    Replies:
    4
    Views:
    471
    Tomasz Krol
    May 10, 2010
  5. Just Another Victim of the Ambient Morality

    What's the difference between these two block passing strategies?

    Just Another Victim of the Ambient Morality, Jul 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    97
    Erik Veenstra
    Jul 20, 2006
Loading...

Share This Page