Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Identifiers

 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just compiled this without any problem ...
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Newbie Java, may I ask you to register with proper name?
You can read this post for more details...
 
Baswa Shaker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a CLASS-NAME and an OBJECT-NAME be same?
If yes then the code should compile without any errors.
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic