aspose file tools*
The moose likes Beginning Java and the fly likes object has the same name as class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object has the same name as class" Watch "object has the same name as class" New topic
Author

object has the same name as class

sumit kar
Greenhorn

Joined: Aug 30, 2005
Posts: 1
what happens
when the OBJECT HAS THE SAME NAME AS THE CLASS NAME
example:Test Test=new Test();

WHAT R THE VARIOUS PROBLEMS WE WILL FACE
AND HOW DOES THE COMPILER INTERPRET THIS IN DIFFERENT TYPES OF STATIC AND NON-STATIC METHOD CALLS.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

THERE IS NO PROBLEM WITH THIS AT ALL. VARIABLE NAMES AND CLASS NAMES ARE IN ALTOGETHER DIFFERENT NAMESPACES AND DON'T INTERFERE WITH EACH OTHER AT ALL, IN ANY CONTEXT. YOU CAN ALSO HAVE A METHOD WHOSE NAME IS THE SAME AS THE CLASS IT APPEARS IN, SO, FOR EXAMPLE, THE FOLLOWING CODE WOULD BE PERFECTLY LEGAL JAVA:

TEST TEST = new TEST().TEST();

P.S. ARE YOU FROM NIGERIA?

P.P.S. I'M MOVING THIS TO JAVA IN GENERAL (BEGINNER).


[Jess in Action][AskingGoodQuestions]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

My eyes are ringing from all the shouting.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
It's important to note that objects don't have names. It is this very fact that makes it extremely difficult to talk about objects, and results in great confusion. What you have there is an "object reference", which do have names; the object referred to by the reference does not.

This might help: http://qa.jtiger.org/GetQAndA.action?qids=75&showAnswers=true&showCodeLineNumbers=true


Tony Morris
Java Q&A (FAQ, Trivia)
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

As literal and precise as the distinction may be, I don't find it very helpful to insist on saying 'the object (in memory) referred to by "o"' any more than it's helpful to say 'the object (on the floor) referred to by "chair"'.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Michael Ernest:
As literal and precise as the distinction may be, I don't find it very helpful to insist on saying 'the object (in memory) referred to by "o"' any more than it's helpful to say 'the object (on the floor) referred to by "chair"'.


I teach at a university; I see this often:
"my object cannot be null, why do I get NullPointerException?"
"Object x; when is x eligible for garbage collection?"
"Java passes primitive types by value, and objects by reference"
"...but I created the array with 'new', yet the elements are still null"
<insert-a-zillion-other-remarks-here/>

Can you see the problem by not providing the distinction?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Michael Ernest:
As literal and precise as the distinction may be, I don't find it very helpful to insist on saying 'the object (in memory) referred to by "o"' any more than it's helpful to say 'the object (on the floor) referred to by "chair"'.


In a discussion between experienced developers, I'd probably agree.

In a beginner's forum, I'd assume that it helps to be more pedantic, because differentiating between an object and reference variables is a common problem for beginners.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object has the same name as class