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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Identifiers" Watch "Identifiers" New topic
Author

Identifiers

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
Something from my kitchen:
public class Class
{ Class Class = new Class();
public Class Class ( Class Class){
return Class;
}
}
a) The above example will not compile saying that Class Class already exists in package java.lang
b) The above example will not compile because Package name and Class name cannot be same.
c) The above example will not compile because a Class can not have a variable with the same name as the Class name itself.
d) The example compiles if we add line "package Class;" before the Class declaration.
e) The example compiles as it is.
[This message has been edited by mohit joshi (edited October 05, 2000).]
Rong Chen
Ranch Hand

Joined: Aug 07, 2000
Posts: 30
hmm,
lovely example demonstrating that "A simple name("Class" in this case) may occure in contexts where it may potentially be interpreted as the name of a variable, (even a method in this case) a type or a package(from JLS 2nd)".
besides, it will not work if you explicitly import java.lang.Class in the beginning.

Chris Meijers
Greenhorn

Joined: Nov 07, 2000
Posts: 9
Isn't the package 'java/lang.*' imported by default? That should disallow class Class IMHO. And I have my doubts about Class Class declaration as well. int int =0 is probably not allowed either...
Chris Meijers
Greenhorn

Joined: Nov 07, 2000
Posts: 9
Isn't the package 'java/lang.*' imported by default? That should disallow class Class IMHO. And I have my doubts about Class Class declaration as well. int int =0 is probably not allowed either...
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I just compiled this without any problem ...
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Newbie Java, may I ask you to register with proper name?
You can read this post for more details...


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Baswa Shaker
Greenhorn

Joined: Oct 18, 2000
Posts: 17
I am encountering a compile time error: class Class is public, should be declared in a file named Class.java though it is defined as class.java. What's the real problem?
TARIT DASGUPTA
Greenhorn

Joined: Nov 05, 2000
Posts: 14
Can a CLASS-NAME and an OBJECT-NAME be same?
If yes then the code should compile without any errors.
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi Mohit,
i'm not sure, if this is a multiple answer question or not. I would suggest d) and e) as solutions...
Originally posted by mohit joshi:
Something from my kitchen:
public class Class
{ Class Class = new Class();
public Class Class ( Class Class){
return Class;
}
}
a) The above example will not compile saying that Class Class already exists in package java.lang
b) The above example will not compile because Package name and Class name cannot be same.
c) The above example will not compile because a Class can not have a variable with the same name as the Class name itself.
d) The example compiles if we add line "package Class;" before the Class declaration.
e) The example compiles as it is.
[This message has been edited by mohit joshi (edited October 05, 2000).]

Since Class is not a keyword or a reserved word, you can use it as identifier. Same with String String = "String".
For the initialization of the default constructor of the newly created Class-Object is used. For a "normal" Class, one of type java.lang.Class, you can't call the constructor, cause he is not visible.
Even if you define the package as java.lang it is no problem, cause in the worst case you will overwrite the existing java.lang.Class. BUT after you've done that, there could appear some problems
Correct me, if i'm wrong
cheers
Oliver
Seema Shety
Greenhorn

Joined: Nov 08, 2000
Posts: 1
if i include "import java.lang.Class; "
get an error :
Class is already defined in empty package
import java.lang.Class;
^
1 error
Finished
Can some one explain "empty package" means ?

if i include "import java.lang.* " . no error .
what's the difference in two import statement. (ya i do know on includes all class files of java.lang and other imports specifically Class)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Identifiers