How a method can be called without object reference - java

I am learning android and I am newbie. I encountered a code i.e.
FramgmentManager fm = getFragmentManager();
I know that getFragmentManager() is a method defined in Activity class.
In java we access method through object reference that is obj.method() but here how can we call getFragmentMananger without any object refrence

In your case there is an 'automatic' this reference meaning that your method is called on the enclosing object you're using right now (an Activity).

Related

difference between working of early binding and late binding in java?

I would like to share first what i know and probably think...
Binding -> Linking between method call and method defination.
It is said that early binding is the example of Method Overloading and
late binding is the example of Method Overriding
But, i was confused as we know that method is invoked after knowing the object type at run time and object is also created at run-time. So, when we write..
Parent obj = new Parent();
obj.method(int x) // one of the overloaded method
now, then in this case also method call should be determined at run-time?
but, they say method overloading is example of early binding? and it is determined already which method is to be called but then it will interfere with what i've said in the above line(which is object is created and runtime so, method invoking will also be decided at run time)
Now, I am sharing what I think..
the method overloading concept is only holded in class scope so it is definitely known that the method with integer parameter will be called.
but, as the inheritance enters and if that
method(int x)
is also overloaded then it comes to the kind of object is at run time.
even if it is not overridden and it calls like this
Parent obj = new Parent();
I think jvm still checks the method to invoke according to the class instantiated.
but I am confused that what I am thinking is true or not. I would also like to know internal working of this.

calling non-static method without instance variable

I’m trying to figure out the below code. ImageIcone called “non-static method” getImage() according to Java API without any instance variable:
public void paintComponent(Graphics g) {
Image img = new ImageIcon("imgtest.jpg", "description...").getImage();
g.drawImage(img,3,4,this);
}
This
new ImageIcon("imgtest.jpg", "description...")
creates an instance. The getImage() method is invoked on that instance and then all references to that instance are lost. The object can then be garbage collected if it is unreachable.
Remember, all you need to invoke an instance method is an expression that resolves to an object reference. A variable of a reference type is one such expression. A constructor invocation is another such expression. An invocation of a method with a return type of some reference type is also such an expression.
You don't need an 'instance variable.' You only need an instance, and new ImageIcon("imgtest.jpg", "description...") is the instance.
In your code,you are creating an anonymous object of ImageIcon class by using the code new ImageIcon("imgtest.jpg", "description..."). Since you are not going to use that object in future,that's why you are opting to create an anonymous object.

How does the special variable "this" know exactly which Object to refer to in a program in JAVA? [duplicate]

This question already has answers here:
Using the keyword "this" in java [duplicate]
(12 answers)
Closed 9 years ago.
If the special variable this refers to an Object in a variable/method that is being used. How does it know exactly which object it must refer to out of various objects in a program?
The mechanism is almost disappointingly simple.
Each instance method actually takes one more argument than you declare for it, and that extra argument is assigned to this. Java syntax just thinly disguises this. When you write
list.get(0);
you have actually written
get(list, 0);
in a slightly modified way. The Java runtime resolves which get method to call by inspecting the type of that first argument and locating the appropriate get method in its class.
this points to the current object instance that it is used in.
If you define a class A with a method() that contains a this reference then you create two instances of the class
A a1 = new A();
A a2 = new A();
If you call a1.method() then this will refer to a1, if you call a2.method() then this will refer to a2
A a = new A();
a.doSomething(i) // is same as calling doSomething(a, i).
So, internally this refers to "a". The first argument to the function will be the object (there will only be one method that will be used by all objects). So, argument o will be the current object which has called this function.
From JAVA LANGUAGE SPECIFICATION
The keyword this may be used only in the body of an instance method,
instance initializer, or constructor, or in the initializer of an
instance variable of a class. If it appears anywhere else, a
compile-time error occurs.
When used as a primary expression, the keyword this denotes a value
that is a reference to the object for which the instance method was
invoked (§15.12), or to the object being constructed.
The type of this is the class C within which the keyword this occurs.
At run time, the class of the actual object referred to may be the
class C or any subclass of C.
The keyword this is also used in a special explicit constructor
invocation statement, which can appear at the beginning of a
constructor body (§8.8.7).
You can also refer to Oracle Tutorials
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
Oracle Java Tutorials
this is a very important keyword that can differentiate between parent and child class objects. this refers to the present context in which object has too be referred to..!!

clarification of "this" keyword in Java

I have this code copied from Android developers website:
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
I am wondering what exactly "this" keyword refers to? Does it refer to the class "ExampleActivity"? And in general how to find what "this" refers to?
It refers to the instance of ExampleActivity on which onCreate() has been called.
In general, from the Java Language Specification, 15.8.3:
The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.
When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed. The type of this is the class C within which the keyword this occurs. At run time, the class of the actual object referred to may be the class C or any subclass of C.
this refers to the most inner class instance. In your example it refers to ExampleActivity which is of type OnClickListener which is passed in to setOnClickListener.
Within an instance method or a
constructor, this is a reference to
the current object — the object whose
method or constructor is being called.
You can refer to any member of the
current object from within an instance
method or a constructor by using this.
Reference (from the Sun Java Tutorial):
Using the this keyword
Understanding Instance and Class
Members
"this" is a reference to the current object.
In your case, it refers to an instance of the ExampleActivity class.
http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html
Yes, 'this' refers to the instance of the enclosing class.

Invoking dynamic Method without known Constructor?

If I do this in Java to call a method name from a class dynamically, it works.
MainApp app = new MainApp();
Method meth = app.getClass().getMethod("myMethod", MyParameterType.class);
//call method
meth.invoke(app, new MyParameterType("hello"));
But this worked because I know the constructor in the invoke method. But if I were to pass the Method object as a parameter to some other classes, and I don't know who is the constructor, I cannot invoke the method any more. Even if I know, I may not want to create a different object to just make a call to the method. For eg:
//This is in the class call MainApp.java.
//There is a method in MainApp.java that looks this way: myMethod(MyParameterType param);
MainApp app = new MainApp();
OtherClass myClass = new OtherClass();
Method meth = app.getClass().getMethod("myMethod", MyParameterType.class);
myClass.callMe(meth);
//Inside OtherClass.java
public void callMe(Method meth) {
//call method
meth.invoke(########, new MyParameterType("hello"));
}
In this case, what should I put for the ######## parameter? Within the context of OtherClass.java, the base constructor object wouldn't be known. And why would I need if since meth is already a Method type that I just call like a function?
Thanks
Assuming it's an instance method, you've got to have an instance to call the method on, just like anything else. How you get hold of that instance will depend on what you're trying to do; you could pass in a Constructor, or a reference to an existing object, or some interface which will create the instance when you ask it to... we can't really give you any advice on which approach is the most suitable without knowing what you're trying to do.
If it's a static method, you can pass null for the first argument.
What it seems you are looking for or thinking about is the concept of `lambda functions``. Those can be called in isolation.
A Method type is not a standalone method, but more like a 'path' into an object. Compare this with a relative URL like /subscribe.html. Out of context this is pretty useless, but when bundled with a site like www.example.com it makes sense.
As such, Method can only be used in combination with an instance. (edit: as John mentioned, unless it's a static method of course which do not need instances)
If you can safely invoke a method without providing an instance, it should be a static method, in which case any instance provided is ignored, you can give it null.
If you have to provide an instance of the object, there is no way around this.
If the developer who write the method has labelled it non-static incorrectly, I suggest you discuss with them why they did it.

Categories