aspose file tools*
The moose likes Beginning Java and the fly likes class types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class types" Watch "class types" New topic
Author

class types

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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 ]

May the force of the Java be in all of us !!!
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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)!
Guru Gulab Khatri
Greenhorn

Joined: Feb 06, 2003
Posts: 5
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

Joined: Jan 07, 2003
Posts: 92
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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

Joined: Jan 07, 2003
Posts: 92
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

Joined: Jan 07, 2003
Posts: 92
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();
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class types