File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner classes" Watch "inner classes" New topic
Author

inner classes

Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
hi everybody,
i have two questions here:
1. there is some code and quotations from my book:
//: c08: Destination.java
public interface Destination {
String readLabel();
} ///:~
"... what if you need to perform some constructor-like activity? "(for the inner class, they mean).
"With instance initialization, you can, in effect, create a constructor for an anonymous
inner class:

Inside the instance initializer you can see code that couldn’t be executed as part of a field
initializer (that is, the if statement). So in effect, an instance initializer is the
constructor for an anonymous inner class."
a)why i must have this parenthesis before "cost = Math.round(price);" and after "System.out.println("Over budget!");"? why it doesn't work without them?
b)if statement really can't be executed as part of a field initializer?
2.thanks to all who tried to help me with my last question (about gc). now i know that there is the option verbose:gc, but how to use it? "java -verbose Class" (a class name) works, but "java -vebose gc" - doesn't.
Sorry, it was long... And tnx in advance.
Helen
[ edited to preserve formatting using the [code] and [/code] UBB tags and remove smilies -ds ]
[ June 30, 2002: Message edited by: Dirk Schreckmann ]
Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
i don't know why the Graemlins took the places of the letters. it was Destination and Parcel, of course.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Helen,
Note that you can preserve most code formatting by surrounding the code with the [code] and [/code] UBB Tags and that you can turn off the smilies by selecting the appropriate checkbox towards the bottom of the submit post page.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
thank you, Dirk, it looks much better now.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
java -vebose:gc ClassName


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
a)why i must have this parenthesis before "cost = Math.round(price);" and after "System.out.println("Over budget!");"?

parenthesis? Are you talking about the curly braces? That is your instance initializer inside your inner class.

if statement really can't be executed as part of a field initializer?

An if statement can be, but price and cost are not defined until you're inside the constructor.

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Helen Tal:
hi everybody,
a)why i must have this parenthesis before "cost = Math.round(price);" and after "System.out.println("Over budget!");"? why it doesn't work without them?

This is a class initializer. ANything enclosed in braces {} will be executed when the class is created. However, it will be executed BEFORE the constructor, so I don't see why he called this a inner class instance constructor since the constructor WILL execute and MAY overwrite any variables you just initialized in your class initializer.
Further if you wanted a inner class constructor just make one.

This is what I think. Anyone disagree?
Originally posted by Helen Tal:

b)if statement really can't be executed as part of a field initializer?

I am not sure about this and have never tried it. But why would you put it there instead of in the constructor anyway? I don't even know why this is legal in Java, what use is it?
Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
thank you, Marilyn and CL, of course, curly braces. sorry, English is not my mother tongue. i didn't know i can do this kind of initializing in a class, so tnx, i've learned something new.
ok, i understand now what he means when he talks about if statement.
CL, this class can't have a constructor, cause it's an anonymous inner class. that's like i understand this.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by CL Gilbert:
This is a class initializer.

No, it's an instance initializer - for a class initializer you would have to prepend the static keyword.


This is what I think. Anyone disagree?

If you tried it you would probably find out that your compiler strongly disagrees...
Regards, Ilja


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner classes