File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

True or False

 
Sagar Kumbhar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Static methods has no this reference.

is it true or false??
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what happened when you tried it?

Henry
 
Sagar Kumbhar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Kumbhar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction you cannot override a static method it is not just possible.
 
Sagar Kumbhar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes thats more technical
 
AKINLEYE ADEDAMOLA
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction you cannot override a static method it is not just possible.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
geet kaur
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic