Difference between these two class declaration

R

Rach

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() {..}
}
///---------------------------///
 
V

VisionSet

Rach said:
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.
 
A

Andy Fish

Rach said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top