• 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

this Refrence Problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i am new member in your javaranch family and scjp preperation as well as.
i have problem with below code. please check it.

class vipin{
public static void main(String[] arg){
System.out.println("Class Name is"+this.getClass().getName());
}
}

It shows some problem on compilation. but if, i make an object of vipin class and do it like.

class vipin{
public static void main(String[] arg){
vipin obj=new vipin();
System.out.println("Class Name is"+ obj.getClass().getName());
}
}

now,It gives name of class. as, i expected.
So, what is the problem with this refrence. i m using jdk1.4
Please, anybody reply this problem.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its giving an error because the keyword 'this' cannot be used in a static method. Give that statement in another method and call this method from main, it will work.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can not use "this" in "main wich is a static method
because "this" refers to an object and a static method
can only refer to a class (not an instanciation)
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this" and "super" can not be used in static blocks & methods.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic