Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Difference between these two class declaration

Reply
Thread Tools

Difference between these two class declaration

 
 
Rach
Guest
Posts: n/a
 
      06-09-2004
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() {..}
}
///---------------------------///









 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      06-09-2004
"Rach" <(E-Mail Removed)> wrote in message
news:ca6ktm$4du$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Andy Fish
Guest
Posts: n/a
 
      06-09-2004
"Rach" <(E-Mail Removed)> wrote in message
news:ca6ktm$4du$(E-Mail Removed)...
> 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).


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between these two declaration of class t1 Manoj C++ 4 05-10-2010 07:24 PM
what's the difference between these two methods? (aka, why doesn't one of them work?) JohnJSal Python 13 11-04-2006 08:17 PM
difference between these two struct definitions oo@@oo C Programming 1 08-04-2006 02:19 PM
Difference between these two cameras? Tim Chmielewski Digital Photography 8 11-22-2005 11:59 PM
what is the difference between these two declarations Guybrush Threepwood C++ 7 02-26-2004 11:24 PM



Advertisments