This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes class Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class Object" Watch "class Object" New topic
Author

class Object

Ali Sadiq
Greenhorn

Joined: Dec 09, 2008
Posts: 4
Hello,

I was doing some experimenting and found that the following code compiles and runs without any exceptions or errors. But im not sure why.

01 final class Object {
02 Object() {
03 super();
04 }
05 }
06
07 public class ObjectTest {
08 public static void main (String [] args) {
09 new Object();
10 }
11 }

I have the following questions about this:
a) In line "01", how can you declare class Object final, since all other classes implicitly extend it.
b) In line "03", what does the call to super() do, since Object has no superclass?

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

a) In line "01", how can you declare class Object final, since all other classes implicitly extend it.


All other classes extend the Object class, in the java.lang package. You class is a different Object class, that is not in the java.lang package.

b) In line "03", what does the call to super() do, since Object has no superclass?


The super() calls the constructor to the class that your Object class is inheriting from -- the java.lang.Object class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ali Sadiq
Greenhorn

Joined: Dec 09, 2008
Posts: 4
Thanks Henry!

Now, if i add the line:
import java.lang.*;
to the top of my file, it still compiles and runs OK.

However, if i use:
import java.lang.Object;
only then do i get a conflict. Why is this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Ali Sadiq:

Now, if i add the line:
import java.lang.*;
to the top of my file, it still compiles and runs OK.

However, if i use:
import java.lang.Object;
only then do i get a conflict. Why is this?


Because that's how it works. If you import a specific class by name, then any unqualified use of that name always refers to the imported class. If you import a whole package with a wildcard, then locally-defined names (like your Object class) take precedence.

Note that defining a class called Object -- or indeed, a class with the same name as any class in java.lang or java.util -- is a terrible idea.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class Object
 
Similar Threads
synchronization and _final_ modifier
Overloading methods (null argument)
Question about Final class
Garbage collection
I know local objects are thread safe. But why not obj here too?