*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static modifiers" Watch "static modifiers" New topic
Author

static modifiers

Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
What does it mean to have a static class with public or private access? Since the method is static and the class must be static doesn't the static class belong only to the class MyClass1. That is it cannot be overriden.
public class MyClass1 {
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}
What modifiers would be legal at XX in the above code?
1) public
2) private
3) static
4) friend
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
I think the answer is 1, 2 and 3.
Here MyInner is a member inner class, it just like a member variable or member method and can use all the modifiers that member variable or member method can use.

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
Is the choice 'public' is correct? Already Myclass is a public class.
Anyone explain this?
Vanitha.
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Hi, I think that public is correct. Just as you can have public member variable or public member method in a public class.
In fact, you can even have public member variables or public member methods or public member classes in a class with "default" accessibility. At least there is no problem for compilation. But as the class is just visible within the package, its public members are only visible within the package although they have the public access modifier.
I think this is correct but not sure. correct me if I'm wrong.
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
one java file can't have more than one top-level public class.
But top-level public class can have public member inner class(es).
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Charlie,
You are getting messed up with syntax. Methods can be overridden not classes. Classes can only be extended. The question deals only with classes therefore overridding doesn't even come into the answer.
The idea behind static or non-static inner classes refers to the need for a parent class. For example if we have the following:

The idea of public or private has to do with the creation of the inner class from outside the outer class (or package). For example suppose we have the following file:

Hope this helps clear it up!
Manfred.
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Hi thanks all!
I still have a question on this. The above static class was created in a static main method. What are the limitations on access to the static class due to this?
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Hi the correct answers are public, private and static. This was a question on JQ+.
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
The static method below had a static class defined in it. But the compiler did not like it? Why?
public class test {
static void staticMethod()
{
// The below line generates a compile error
// static class StaticInner {}
}
//In reponse to questin above, public class gives no error
public static class StaticOuter {
static void message() {
System.out.println("StaticOuter");
}
}
public static void main(String args []) {
StaticOuter.message();
}
}
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
The fact is:
Methods, no matter static or not, cannot have static members. Think of the inner class as a 'member' of the enclosing method.
The reason is:
The scope of the method is local. When the method call returns, everything inside becomes out of scope. So you cannot define a class level member inside.
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
Hi,
Originally posted by Kevin Yip:
The fact is:
Methods, no matter static or not, cannot have static members. Think of the inner class as a 'member' of the enclosing method.
The reason is:
The scope of the method is local. When the method call returns, everything inside becomes out of scope. So you cannot define a class level member inside.

It's OK for methods to have local classes, only they can't be public, private, protected, or static. Since the scope is restricted to the method, it doesn't make sense to have them be public, protected, or private as the issue of outside access doesn't arise. Static doesn't make sense either because the class doesn't persist when the method exits.
The same is true of local variables too.
Ranjan
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Originally posted by Charlie Swanson:
The static method below had a static class defined in it. But the compiler did not like it? Why?

the rules is quiet simple :

  1. Everything inside method will be considered as a local scope no matter they are static or non-static,
  2. due to rule no:#1, They are always accessible within the block in which they are declared, it means outside the scope are not allow to access it.
  3. hence, the compiler doesn't allow you to specify the visibility of local variables, or local class..., as we know, they can only been access within the block in which they are declared !!

  4. java compiler is smart enough right... ?
    hth
    stevie
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
i think only final modifier can be attached with a local inner class
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Hello.
Thanks for clearing this up!
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Hello Charlie,
I want to say one more thing. In your sample code, the inner class is not within the static main method. It's a member inner class of class MyClass1.
------------------------------------------
public class MyClass1 {
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}
--------------------------------------------
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Bin,
You are exactly right!
When I first read the question, I read it wrong. But I obviously did not understand something. That is why I sent another program, and asked questions regarding it.
I can't thank everyone enough for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static modifiers