• 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

class types

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are the different types of classes that are possible in java?
Can I write a class like this and then have another class within?

I could compile and run the above code only with line 2. Line 1 would not allow a compilation. Question is why? If the class inner is declared static, it will be surely beaccessible through the name of its enclosing class, but why isnt it also accesible through an instance of the enclosing class?
For example, can you not access the value of a static variable both through an instance of its enclosing class as well as through just its class name? Isnt the rule 'a static class cannot access a non static variable or method but a nonstatic class can access a static variable or method?"
[ February 08, 2003: Message edited by: Shashank Gokhale ]

(Marilyn added code tags)
[ February 08, 2003: Message edited by: Marilyn de Queiroz ]
[ February 10, 2003: Message edited by: Shashank Gokhale ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is true!! if u have a static inner class, surely it will be accessible (where the outer class is accessible) by an object created with the class or by the class (outer in this case)!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by leandro oliveira:
It is true!! if u have a static inner class, surely it will be accessible (where the outer class is accessible) by an object created with the class or by the class (outer in this case)!


You cannot access the inner class directly through the outer class object normally also so if you want to access the inner class you will have to declare an object of inner class in the outer class.
Here is the modified solution.
class outer
{
int i=99;
static int j=3;
static class inner
{
static void say()
{
System.out.println("A nested inner class is called via outer.inner as no instance can be created of a static class");
}
}
inner ao = new inner(); //My Added Code
}
public class classes
{
public static void main(String arg[])
{
outer o=new outer();
o.ao.say(); //line 1
outer.inner.say(); //line 2
}
}
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, in the following code
import java.awt.event.*;
class anonymousClass
{}
class outer
{
int i=99;
static int j=3;
final int k=98;
class memberClass
{
void say()
{
System.out.println("Can only access instance (non static) variables of the enclosing class. int i: "+i);
System.out.println("A member class cannot be static and cannot have static declarations of methods or variables");
System.out.println("A member class is called through an instance of its enclosing class or via new outer().new inner()\n");
}
}
static class nestedClass
{
static void say()
{
System.out.println("Can only access static variables of the enclosing class. Static int j: "+j);
System.out.println("A nested class is static and all its methods and variables should be declared static.");
System.out.println("A nested class is called via outer.inner as no instance can be created of a static class\n");
}
}
void dummyMethod()
{
int l=21;
final int m=99;
class localClass
{
void say()
{
System.out.println("Local classes cannot have the modifiers public, private, protected or static");
System.out.println("Local classes can only access non static variables of the top level class, int i: "+i);
System.out.println("Local classes can access only static variables of the top level class, static int j: "+j);
System.out.println("Local classes can access only final variables declared in the enclosing method and all");
System.out.println("variables of the top level class: top level variables int i="+i+" static int j="+j+"final k="+k);
System.out.println("and local variables of enclosing method: final int m="+m+". int l cannot be accessed as it is not final");
}
}
}
}
public class classes
{
public static void main(String arg[])
{
new outer().new memberClass().say();
outer.nestedClass.say();
// new outer().dummyMethod().new localClass().say();
}
}
how can I access the say() method of the localClass class which is in dummyMethod() of class outer?
I tried the above commented line in my ode but it prevented compilation.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot access a local class outside of the method which contains it. This is the same as with local variables.
[ February 10, 2003: Message edited by: Layne Lund ]
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so why it compiles under j2sdk 1.4??? I tested, the following code compiles and run:
class outer{
int i=99;
static int j=3;
static class inner{
static void say(){
System.out.println("A nested inner class is called via outer.inner as no instance can be created of a static class");
}
}
}

public class MyClass2{
public static void main(String arg[]) {
outer o=new outer(); //this ok
o.inner.say(); //line ok
outer.inner.say(); //line ok
}
}
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Leandro,
I was asking about local classes, that is classs enclosed within methods.
A nested class will be accessible outside its outer class as you have seen from your example.
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I seem to recall that local classes can access all variables of the outer class but only final variables of the enclosing method. Yet when I run the following, it seems the say() method of localClass can access even the non final variables of its enclosing class dummyMethod() in addition to all variables of the outer class.
Whats going on?
import java.awt.event.*;
class anonymousClass
{}
class outer
{
int i=99;
static int j=3;
final int k=98;
class memberClass
{
void say()
{
System.out.println("Can only access instance (non static) variables of the enclosing class. int i: "+i);
System.out.println("A member class cannot be static and cannot have static declarations of methods or variables");
System.out.println("A member class is called through an instance of its enclosing class or via new outer().new inner()\n");
System.out.println("***********************************************************************");
}
}
static class nestedClass
{
static void say()
{
System.out.println("Can only access static variables of the enclosing class. Static int j: "+j);
System.out.println("A nested class is static and all its methods and variables should be declared static.");
System.out.println("A nested class is called via outer.inner as no instance can be created of a static class\n");
System.out.println("***********************************************************************");
}
}
void dummyMethod()
{
int l=21;
final int m=99;
class localClass
{
void say()
{
System.out.println("Local classes cannot have the modifiers public, private, protected or static");
System.out.println("Local classes can only access non static variables of the top level class, int i: "+i);
System.out.println("Local classes can access only static variables of the top level class, static int j: "+j);
System.out.println("Local classes can access only final variables declared in the enclosing method but all");
System.out.println("variables of the top level class: top level variables int i="+i+" static int j="+j+"final k="+k);
System.out.println("and local variables of enclosing method: final int m="+m+". int l cannot be accessed as it is not final");
System.out.println("***********************************************************************");
}
}
new localClass().say();
}
}
public class classes
{
public static void main(String arg[])
{
new outer().new memberClass().say();
outer.nestedClass.say();
new outer().dummyMethod();
}
}
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic