• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very very Very important to Understand this Inner class

 
Hussain
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Hussain
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why does Q3 not compile and say undefined variable B
Please help
Regards
Neha
 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a twist to one of the above code samples. Guess the result before trying to compile &/or run.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent work nain!
i got it as well
thankxxxxxxxxxxxxx
------------------
Muhammad Hussain
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Hussain
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Nain.
I will practice codes based on this now.
Regards
neha
 
Vinny Chun
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic