[HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Moderator: admin
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
[HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
"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
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
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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.
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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
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
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Creating a class inside a method is very common while adding handlers for events in GUI components. For example:
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:
But as the book says, you can't apply the static keyword to members of a method. So, you can't make MyWindowListener static.
Code: Select all
myFrame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("Window closing");
System.exit(0);
}
} );
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);
...
}
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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() {}
}
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Section 8.1.3 of JLS mandates the following:
So, as per point 2 above, you can have static variable as long as it is a constant variable.
(The above is not important for the exam).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.
So, as per point 2 above, you can have static variable as long as it is a constant variable.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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!
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!
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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 

-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
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. 

-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]
Yeah, that's a bummer.
Who is online
Users browsing this forum: No registered users and 5 guests