Page 1 of 1

About Question enthuware.ocpjp.v7.2.1361 :

Posted: Wed Jun 12, 2013 12:53 pm
by emj211
Thanks for the detailed explanation here....I am confused by this statement...

"static public class B //Static Nested class . It can be used in other places: A.B b = new A.B(); There is no outer instance."

Isn't 'new A...' creating an outer instance?

Thanks.

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Thu Jun 13, 2013 8:09 am
by admin
No, only an instance of B is created.

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Fri Sep 20, 2013 5:50 am
by The_Nick
Hi,
You say in the explanation:
A class defined inside an interface is implicitly static.
Did you actually mean An interface not a class.. isn't it?

The_Nick.

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Fri Sep 20, 2013 5:59 am
by admin
No, it means class defined in an interface. For example:

Code: Select all

interface I{
  class X {  <-- This class is implicitly static, no need for static keyword

  }
}

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Fri Oct 31, 2014 4:28 am
by vijayanand
when i do javap on I1, it displays only mA()

I1.Inner1 is not displayed.

Why javap in my computer is behaving like this?

------------------
Compiled from "I1.java"
public interface I1{
public abstract void mA();
}

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Fri Oct 31, 2014 6:53 am
by admin
That is just how the the java language designers implemented it. The class file for the inner class is created by the compiler in another file (check the directory for other files). javap doesn't show the inner classes.

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Sat Jan 24, 2015 12:35 pm
by Svetopolk

Code: Select all

public interface I1
{
    public void mA();
    public interface InnerI1
    {
        int k = 10;
        public void innerA();
    }
    public class InnerI2
    {
        int k = 10;
        public void innerA(){};
    }
}
My result it not coincide with yours. Neither interface nor class (inside outer interface) is marked as static.
Please clarify my mistake:

Code: Select all

javap I1$InnerI1.class
Compiled from "I1.java"
public interface I1$InnerI1 {
  public static final int k;
  public abstract void innerA();
}

javap I1$InnerI2.class
Compiled from "I1.java"
public class I1$InnerI2 {
  int k;
  public I1$InnerI2();
  public void innerA();
}

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Sun Jan 25, 2015 7:51 am
by admin
Your javap result show that neither of the classes have a reference named "this$0" of type I1. This itself means that both the classes are top level classes (and not inner classes). Thus, they are both implicitly static for the enclosing class/interface. The keyword static will never be visible on class definition.
You should test it as follows:

Create a static as well as non-static class inside a regular class and then do javap on both. You will see that the javap output for the static class is same as the class defined inside an interface without the static keyword.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Sat Oct 17, 2020 6:18 am
by noeloo
1. Can there be non-static interfaces? I found on https://docs.oracle.com/javase/tutorial ... asses.html that "interfaces are inherently static". So what sense does it make to write about "static interface"?
2. Is javap in scope of 1Z0-819 exam?

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Sat Oct 17, 2020 6:48 am
by admin
It doesn't make sense for interfaces to be non-static because you can never have an instance of an interface. You always have instance of some class. If there can never be an instance of an interface, then it is inherently static!

No, javap is not on the exam. But you can use that tool for understanding concepts.

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Thu Nov 19, 2020 2:42 pm
by dimitrilc
For this line,
A static nested class can contain non-static member variables.
Did you mean non-static member variables within itself? Or including those from the outer class?

Like this?

Code: Select all

class OutTest{
    public int i; //non-static member variable
    
    static class InnerStaticTest{
        public int j = i; //won't compile. Cannot make a static reference to the non-static field i
    }
}
or like this?

Code: Select all

class OutTest{
    static class InnerStaticTest{
        public int j;
    }
}

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Thu Nov 19, 2020 9:35 pm
by admin
It means the second example above. The first example would be valid if the statement said, "A static nested class can refer to the non-static member variables of the containing class".

Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Fri Feb 25, 2022 8:30 am
by flex567
Which of this classes is a top level class?

Code: Select all

//In Main.java
public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome");
    }
}

class Main1{}


Re: About Question enthuware.ocpjp.v7.2.1361 :

Posted: Fri Feb 25, 2022 9:53 am
by admin
Well, as per JLS 8 Chapter 8:
A top level class is a class that is not a nested class.
A nested class is any class whose declaration occurs within the body of another class or interface.
So, clearly, in your code, both - Main and Main1 are top level classes.