• 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

Identifiers

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just compiled this without any problem ...
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Newbie Java, may I ask you to register with proper name?
You can read this post for more details...
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic