Page 1 of 1
[HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Fri Feb 08, 2019 8:21 pm
by OCAJO1
"In the case of a method or a field, this keyword must appear immediately before the return type of the method or the type of the field respectively."
I don't buy this for a method or a field.
static public Object method(); works just a s well as public static Object method();
static public int i = 5; works just as well as public static int i = 5;
Same goes for,
"In case of nested type definitions, the static keyword must appear immediately before the type."
I wonder if these rules are from older version of Java?
Question,
Last part of 8.3.1 example places a class in the main() method. What would be a case for having a class inside a method()? Thanks
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Sat Feb 09, 2019 2:35 am
by admin
Section 8.3.1 of JLS recommends (but does not require) that the order of other modifiers be : public protected private static final transient volatile
So, the word "immediate" is incorrect. It should just be "before" because you can't do this: public Object static method(); Any other order is fine.
Added to errata.
The book does note near the end of section 9.2.3 page 244 that the exam does not try to trick you on the order of access modifiers and final and abstract keywords.
HTH,
Paul.
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Sat Feb 09, 2019 3:00 pm
by OCAJO1
Question,
Last part of 8.3.1 example places a class in the main() method. What would be a case for having a class inside a method()? Thanks
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Sat Feb 09, 2019 10:03 pm
by admin
Creating a class inside a method is very common while adding handlers for events in GUI components. For example:
Code: Select all
myFrame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("Window closing");
System.exit(0);
}
} );
The above code defines as well as instantiates an anonymous inner class (Not important for OCAJP exam).
You can also define a regular class in a method if you want to create multiple objects of it:
Code: Select all
public static void main(String[] args) {
class MyWindowListener implements WindowListener{
...implement all required methods
}
MyWindowListener wl1 = new MyWindowListener();
frame1.addWindowListener(wl1);
MyWindowListener wl2 = new MyWindowListener();
frame2.addWindowListener(wl2);
...
}
But as the book says, you can't apply the static keyword to members of a method. So, you can't make MyWindowListener static.
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Mon Feb 11, 2019 1:52 pm
by OCAJO1
I'm trying to get my head around why would the following not compile unless class B becomes static class B? Thanks
Code: Select all
public class A {
class B {
static int a;
static String s;
static void methodS() {}
}
}
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Mon Feb 11, 2019 11:46 pm
by admin
Section 8.1.3 of JLS mandates the following:
It is a compile-time error if an inner class declares a static initializer (§8.7).
It is a compile-time error if an inner class declares a member that is explicitly or implicitly static , unless the member is a constant variable (§4.12.4).
An inner class may inherit static members that are not constant variables even though it cannot declare them.
(The above is not important for the exam).
So, as per point 2 above, you can have static variable as long as it is a constant variable.
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Tue Feb 12, 2019 1:05 pm
by OCAJO1
So according to rule 2 - by making class B a static class, the compiler is allowing both its variables and methods to be static, because it considers them to be complier constants now? Since I still don't get it, would you please elaborate.
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Tue Feb 12, 2019 9:33 pm
by admin
No, according to rule an inner class can have only a final static variable. It cannot have a static method.
Not sure what you need elaborated. It is just a rule of the language. If you want to have a static member in an inner class, it has to be a constant variable. If the nested class is a static class, then it can have any static member. That's pretty much it!
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Wed Feb 13, 2019 3:09 pm
by OCAJO1
Oh, so the rules of Section 8.1.3 of JLS (in this case, the second one) do not apply to items with a static inner class. Or is that too broad of an interpretation?
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Wed Feb 13, 2019 9:24 pm
by admin
The rule is for an "Inner class", which means it is not for a static nested class. There is nothing like "static inner class". There is "static nested class". You probably skipped "section 1.9 Nomenclature" of Fundamentals book that explains the terminology

Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Thu Feb 14, 2019 1:42 pm
by OCAJO1
No I went through 1.9. what happens is I keep forgetting Java refers to classes inside another class differently depending on whether they're static or not.

Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Posted: Thu Feb 14, 2019 9:57 pm
by admin
Yeah, that's a bummer.