aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes True or False Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "True or False" Watch "True or False" New topic
Author

True or False

Sagar Kumbhar
Greenhorn

Joined: Oct 27, 2007
Posts: 17
Hi,

Static methods has no this reference.

is it true or false??


SCJP 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Well, what happened when you tried it?

Henry


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

Joined: Oct 27, 2007
Posts: 17
i think for static method we use class name instead of any perticular instace of class to invoke, so this should be true.
but now i don't have anything to execute and confirm it. thats why i asked.
Stijn Janssens
Greenhorn

Joined: Sep 09, 2008
Posts: 12
Static methods or variables are never accessible with this since this refers to the instance and statics don't belong to an instance. You access a static member by using the . operator on the class name.

Strangely, it is also possible to use an object reference variable to access a static member but this is more a syntax trick since you're using the object reference variale to access the static and not the object itself.


<a href="http://zerotreize.com/#/blog/java-certification-matters" target="_blank" rel="nofollow">Java Certification matters?</a><br /><a href="http://www.zerotreize.com" target="_blank" rel="nofollow">Zerotreize.com</a>
Sagar Kumbhar
Greenhorn

Joined: Oct 27, 2007
Posts: 17
but if we hide static method in subclass and try to access it using this.
i think it will give error.
[ September 09, 2008: Message edited by: Sagar Kumbhar ]
AKINLEYE ADEDAMOLA
Greenhorn

Joined: Sep 08, 2008
Posts: 10
Correction you cannot override a static method it is not just possible.
Sagar Kumbhar
Greenhorn

Joined: Oct 27, 2007
Posts: 17
yes thats more technical
AKINLEYE ADEDAMOLA
Greenhorn

Joined: Sep 08, 2008
Posts: 10
Correction you cannot override a static method it is not just possible.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
I like Henry's question...

"What happened when you tried it?"

Let's see some sample code! Let's discuss what that code means and how the compiler and / or JVM deals with the code!!!

The most successful candidates write LOTS of code and explore and experiment.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

I think this thread is going the wrong way. Static methods can be called with this or any other reference of a class. It's just that you cannot use this inside a static method....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
i called a static method with a this ...but its showing a compiler error
that non-static varaible this cannot be referenced in static context...but i don't understand this..please explain
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

You cannot use this in a static method. You must have called a static method with this in the main method. Here's a code that will compile fine

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: True or False