About Question enthuware.ocpjp.v7.2.1361 :

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

Moderator: admin

Post Reply
emj211
Posts: 5
Joined: Wed Apr 17, 2013 2:41 pm
Contact:

About Question enthuware.ocpjp.v7.2.1361 :

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

No, only an instance of B is created.
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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

  }
}
If you like our products and services, please help us by posting your review here.

vijayanand
Posts: 10
Joined: Fri Sep 26, 2014 8:40 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Location: Moscow
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

dimitrilc
Posts: 35
Joined: Sat Jun 06, 2020 4:51 pm
Contact:

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

Post 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;
    }
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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".
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

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


admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests