About Question enthuware.ocpjp.v7.2.1361 :
Moderator: admin
-
- Posts: 5
- Joined: Wed Apr 17, 2013 2:41 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1361 :
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.
"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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
No, only an instance of B is created.
If you like our products and services, please help us by posting your review here.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
Hi,
You say in the explanation:
The_Nick.
You say in the explanation:
Did you actually mean An interface not a class.. isn't it?A class defined inside an interface is implicitly static.
The_Nick.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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.
-
- Posts: 10
- Joined: Fri Sep 26, 2014 8:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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();
}
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();
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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.
-
- Posts: 22
- Joined: Sun Nov 18, 2012 1:51 am
- Location: Moscow
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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(){};
}
}
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();
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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.
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.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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?
2. Is javap in scope of 1Z0-819 exam?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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.
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.
-
- Posts: 35
- Joined: Sat Jun 06, 2020 4:51 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
For this line,
Like this?
or like this?
Did you mean non-static member variables within itself? Or including those from the outer class?A static nested class can contain non-static member variables.
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
}
}
Code: Select all
class OutTest{
static class InnerStaticTest{
public int j;
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
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{}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1361 :
Well, as per JLS 8 Chapter 8:
So, clearly, in your code, both - Main and Main1 are top level classes.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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 112 guests