• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes & Interface - Guoqiao's 3rd Mock

 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In Guoqiao sun's 3rd mock exam , Q03-17 , i changed da code a lill as following :
public class T017 //1
{
public static interface I1{ } //2
public interface I2{ }
}
interface I //3
{
public static class C1 {} //4
public class C2 {}
}

To my surprise , this code compiles okay .
My understandiing is :
-Interface I1 is a static member of class , okay .
-Interface I2 seems like a non-static inner-interface. whts dat ? i guess , non-static inner interfaces are not allowed?
-Class C1 is a static-nested class in top-level interface I , okay.
-Class C2 is not declared static.. hmmm.. so is it by default static , like other members of interface ?
plz help

------------------
Gagan (/^_^\)
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The interface I2 is static and abstract implicitly.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YOu can define non-static interfaces in a class..no problem!
for ur second qu. afaik class c2 wouldn't be static.
ashok.

Originally posted by Gagan Indus:

In Guoqiao sun's 3rd mock exam , Q03-17 , i changed da code a lill as following :
public class T017 //1
{
public static interface I1{ } //2
public interface I2{ }
}
interface I //3
{
public static class C1 {} //4
public class C2 {}
}

To my surprise , this code compiles okay .
My understandiing is :
-Interface I1 is a static member of class , okay .
-Interface I2 seems like a non-static inner-interface. whts dat ? i guess , non-static inner interfaces are not allowed?
-Class C1 is a static-nested class in top-level interface I , okay.
-Class C2 is not declared static.. hmmm.. so is it by default static , like other members of interface ?
plz help


 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes in the above code, class C1 is taken as a static member of
the interface.
The interface by default is abstract
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Every field declaration in the body of an interface is implicitly public, static, and final." JLS 9.3
Therefore it seems that in class C2 will automatically be public, static, and final.

Also, "Member interfaces are always implicitly static." JLS 8.5.2
so it would seem that in I2 is static whether or not you declare it as such.
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankx Angela ,Ashok & Marilyn for ur help !
got my basics clear now !
Marilyn thankx for da JLS links , they been really helpful

------------------
Gagan (/^_^\)
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Under what conditions we will be using the following:
1. interface containing Member classes
2. classes containing member interfaces.
Can you give some practical examples???
Thanx,
Bala.
[This message has been edited by Bala Rajamani (edited September 19, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bruce Eckel has some examples of the use of nested interfaces and classes
http://codeguru.earthweb.com/java/tij/tij0080.shtml#Heading217
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
All the interfaces nested wihtin a class are implicetely static
All the classes nested whitin an interface are implicetely static
javap shows:


C:\Java\TIJ2\Test>javap I
Compiled from T017.java
interface I
/* ACC_SUPER bit NOT set */
{
public static class I. C2 extends java.lang.Object
/* ACC_SUPER bit NOT set */
{
public I.C2();
}
public static class I. C1 extends java.lang.Object
/* ACC_SUPER bit NOT set */
{
public I.C1();
}
}


and


C:\Java\TIJ2\Test>javap T017
Compiled from T017.java
public class T017 extends java.lang.Object {
public T017();
public static interface T017. I2
/* ACC_SUPER bit NOT set */
{
}
public static interface T017. I1
/* ACC_SUPER bit NOT set */
{
}
}


I placed both static and non static interfaces nested within an interface and


C:\Java\TIJ2\Test>javap I
Compiled from T017.java
interface I
/* ACC_SUPER bit NOT set */
{
public static interface I. C2
/* ACC_SUPER bit NOT set */
{
}
public static interface I. C1
/* ACC_SUPER bit NOT set */
{
}
}


So the conclusion is that a class keeps the non static characteristic of a nested class. But all the other combinations result in static whether declared or not.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic