5 Replies - 54962 Views - Last Post: 14 June 2012 - 06:15 AM Rate Topic: -----

#1 -fedexer-  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 03-June 08

this.getClass() can someone explain?

Posted 18 October 2008 - 09:35 AM

Right well i am working through a java book just now for creating an online game, but i have come across a piece of code which i do not understand the functionality of, as in i do not know what it is doing and was wondering if someone could explain it to me?

The code is
this.getClass().getResource(filename);
and the main part i am confused about is: this.getClass()

It is used in this context:

public class PlayMusic extends Applet {
	String filename = "woah.wav";
	Sequence song;
	
	private URL getURL(String filename){
		URL url = null;
		try{
			url = this.getClass().getResource(filename);
		}
		catch(Exception e){
			System.out.println("Exception : " + e);
		}
		return url;
	}


Well if someone could explain the use and what this.getClass() does (and why it's needed) i would be greatful, i like to know what and why i am using code, and so far i can't find anything on the tinterweb to explain it.

Thanks
-fedexer-

Is This A Good Question/Topic? 2
  • +

Replies To: this.getClass() can someone explain?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: this.getClass() can someone explain?

Posted 18 October 2008 - 11:37 AM

*
POPULAR

No problem... getClass() method is a method of Object. Remember that Object is the base class of all objects in Java, so essentially all classes that are in Java and you create inherit this method automatically. What it does is returns a class object that represents the type of class it is.

For example...

 // here we have an instance of our object. It is a MyObject type.
MyObject obj = new MyObject();

// We get the class for this object, so myClass is of type Class.
Class myClass = obj.getClass();

// We ask the class, what is your name?
// It responds by saying I am a "MyObject"
System.out.println("obj is of type " + myClass.getName());



So what your code is doing is saying "In this instance of an object, get the type of object you are and then call its getResource() function"

In context of our example it would be like putting a method in our MyObject class that says "What type of object am I? I am a MyObject class, ok call your getResource() function".

getClass is one of many methods used in what is called "reflection". Reflection is the ability for the language to look at itself and ask itself questions or show data about its objects, methods, classes etc. In your instance you are reflecting off the "this" object (which is the current instance) and asking for it to return the type of object it is.

If you want more information about this method and others, look up the topic "reflection" in the java tutorials online.

I hope that makes sense to you now. Enjoy!

"At DIC we be reflection loving code ninjas... when I look in the mirror I just love what it tells me about myself. It says 'Martyr, you are of type stud' and I must say that I can't disagree." :snap:
Was This Post Helpful? 5

#3 -fedexer-  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 03-June 08

Re: this.getClass() can someone explain?

Posted 18 October 2008 - 12:41 PM

Right i believe i've got it now, and the reason it is used is because at runtime we can't know what the object's type is? And thanks for taking the time to give me such an in-depth explination onj the subject, really cleared alot up for me.

However, say for example i wanted to call getResource without using this.getClass() is it possible? (Just out of general curiousity, not a massive deal).

Thanks again master :ph34r:

This post has been edited by -fedexer-: 18 October 2008 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: this.getClass() can someone explain?

Posted 18 October 2008 - 12:59 PM

In your case, with the code you showed us, it will work because you know for sure that you are in the Applet code

		url = getClass().getResource(filename);


This post has been edited by pbl: 18 October 2008 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 sulav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-May 12

Re: this.getClass() can someone explain?

Posted 14 June 2012 - 03:50 AM

View Postpbl, on 18 October 2008 - 12:59 PM, said:

In your case, with the code you showed us, it will work because you know for sure that you are in the Applet code

		url = getClass().getResource(filename);



how can you call the getClass() method,w/o using the class/object name and a dot operator?I have seen getClass() been used w/o object name and dot operator.How is this possible?
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: this.getClass() can someone explain?

Posted 14 June 2012 - 06:15 AM

View Postsulav, on 14 June 2012 - 12:50 PM, said:

how can you call the getClass() method,w/o using the class/object name and a dot operator?I have seen getClass() been used w/o object name and dot operator.How is this possible?


You can always write methodName() instead of this.methodName(). That's just how Java works. If you don't specify an object for the method to be called on, it will be called on this.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1