Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner classes

 
Helen Tal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know why the Graemlins took the places of the letters. it was Destination and Parcel, of course.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Helen Tal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, Dirk, it looks much better now.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java -vebose:gc ClassName
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic