Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Very very Very important to Understand this Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Very very Very important to Understand this Inner class" Watch "Very very Very important to Understand this Inner class" New topic
Author

Very very Very important to Understand this Inner class

Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
hi all,

as i m posting too much topic these dayz sorry for tht but i feel tht if i can help somebudy then God will also help me....
let me come to the point .....
plz have this code run and understand the behaviouurs
1.
class B
{
static A a=new B().new A();
int i=3;
class A
{
}
}
public class G116
{
public static void main(String [] args)
{
System.out.println(B.a.i);
}
}

2.
class B
{
static B a;
int a(){return 1;}
class A
{
int i=a();
}
}
public class G117
{
public static void main(String [] args)
{
System.out.println(B.a.new A().i);
}
}

3.

interface B //interfaces r implicitly static
{
class A
{}
}
public class G115
{
public static void main(String [] args)
{
B.new A();
}
}
plz understand this before going to exam as these r very likely to come

[This message has been edited by Muhammad Hussain (edited December 06, 2001).]


Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
sorry their is a problem in no.3 use this one
3.

interface B //interfaces r implicitly static
{
class A
{}
}
public class G115
{
public static void main(String [] args)
{
B.new A();
}
}

------------------
Muhammad Hussain
Tony Sam
Ranch Hand

Joined: Nov 19, 2001
Posts: 32
Originally posted by Muhammad Hussain:

{
int i=a();
}

Question 1 and 3 will compile error, but ,i was wondering why question 2 will throw an exception at runtime ,pls someone tell me??
Thanx in advance!

Tony Sam<BR>One want to be a SCJP...
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204

why does Q3 not compile and say undefined variable B
Please help
Regards
Neha


nss
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
Here is a twist to one of the above code samples. Guess the result before trying to compile &/or run.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Tony Sam:
... why question 2 will throw an exception at runtime

Because B.a is initialized to null (as you will see if you run Mike's code), and you can't call a method on a null.

[This message has been edited by Marilyn deQueiroz (edited December 08, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Muhammad Hussain:
3.
<pre>interface B //interfaces r implicitly static
{
class A{}
}
public class G115
{
public static void main( String[] args )
{
B.new A();
}
}
</pre>

inner interfaces are implicitly static
All classes defined in an interface are implicitly static.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hi,
i am not following how Q3 works.
because if i compile the above code i am getting undefined variable B.
Please help
Regards
neha
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Neha,

i am not following how Q3 works.
because if i compile the above code i am getting undefined variable B.

As Marilyn mentioned earlier, all classes defined in an interface
are implicitly static. Their names/types are constructed
by prefixing the types with the name of the enclosing class names.
For instance, the name for class A in interface B of question 3
is actually B.A.
The correct way and the only way to instantiate a static class is by "unqualified" instance creation expression, i.e.
without an expression in front of the "new" operator.
So, I think the statement B.new A() should be changed to:
new B.A();
Give it a try and you will see it compiles.
[This message has been edited by Nain Hwu (edited December 09, 2001).]
[This message has been edited by Nain Hwu (edited December 09, 2001).]
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Nain
Yes it compiles but are we not instantiating interface B by saying "new".
And can classes inside interfaces use interface variables.
bcoz it did not compile when i tried to do it.
Where can i get to read more about classes inside interfaces and how are they used in detail
thanks in advance
Neha
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
Excellent work nain!
i got it as well
thankxxxxxxxxxxxxx
------------------
Muhammad Hussain
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Neha,

There are lots of free tutorials. Here is
one
I also recommend the book "A Programmer's Guide to java Certification" by Khalid & Rolf. It has an excellent chapter on inner class.
[This message has been edited by Nain Hwu (edited December 09, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10

Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
hi all,
to make things simple just understand following

class Ou
{
static class In
{
static int i=0;
}
int j=0;
class In1
{
void add()
{
j++;
}
}
}
public class G157
{
public static void main(String []args)
{
//Ou.In a=new Ou.In();//will compile
//Ou.In a=Ou.new In();//will not compile
/*Ou a=new Ou();
a.new In();*/ //only vairables and object can be uesed as prefix
//System.out.println(new Ou.In().i);//will compile
Ou b=new Ou();
b.new In1().add();
b.new In1().add();
System.out.println(b.j);
//System.out.println(Ou.In1.i);
}
}
bye

------------------
Muhammad Hussain
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
just one more question
interface B
{
int b = 1;
class A {
int c = b;
System.out.println(c);//does not compile here
}
}
public class G115{
public static void main(String [] args) {
B.A a = new B.A();
System.out.println(a.c);//prints 1
}
}
Could some one explain why.
Can't we write a class inside an interface as a normal class having variables and methods and println statements.
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Neha,

Inside a class, only following are allowed:
- members
- instance and static intializers
- constructors
See JLS chapter 8 for details.
So, a statement like System.out.println(c); is not
allowed. Put it inside a method or a
initializer block will work.
[This message has been edited by Nain Hwu (edited December 10, 2001).]
[This message has been edited by Nain Hwu (edited December 10, 2001).]
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanks Nain.
I will practice codes based on this now.
Regards
neha
Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
interface B
{
int b = 1;
class A
{
int c = b;

public void print()
{
System.out.println(c);//does not compile here
}
}
}
public class G115 implements B
{
public static void main(String [] args)
{
A a = new A();
a.print();
}
}
Very cool, Nain! I understand now, thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very very Very important to Understand this Inner class