[HD Pg 190, Sec. 8.3.1 - apply-the-static-keyword-to-methods-and-fields]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
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]

Post 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

admin
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]

Post 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.

OCAJO1
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]

Post 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

admin
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]

Post 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.

OCAJO1
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]

Post 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() {}
   }
   
}

admin
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]

Post 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.

OCAJO1
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]

Post 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.

admin
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]

Post 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!

OCAJO1
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]

Post 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?

admin
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]

Post 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 :)

OCAJO1
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]

Post 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. ;)

admin
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]

Post by admin »

Yeah, that's a bummer.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests