Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class.

public MyClass() {
xMethod();
}


A. a static method
B. an instance method
C. a static method or an instance method

what is the answer a or b or c

B. an instance method.

Sorry, but I am pretty sure if you are asking an answer to an assignment wihout your own understanding within, it this is not the relative place to post this question. But what the heck,

It is b because static is not stated anywhere with the program. It would be static if it
"public static void xMethod()"

Edited 4 Years Ago by Eragah: typo

Ans: B

NOTE:
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.

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class.

public MyClass() {
xMethod();
}


A. a static method
B. an instance method
C. a static method or an instance method

what is the answer a or b or c

the answer is indeed, a, b or c.
why didn't you just tell us what you think it is, and allow us to correct your mistakes (if you had it wrong?)

now: since we already know the answer is B, what do you think the

public MyClass(){
//...
}

is, and what is it used for?

It will take you about 1 minute to write a tiny program to test this. Try it with xMethod declared static or not, and see what the result it.
Tip: I have never known ~s.o.s~ to be wrong, never.

Edited 4 Years Ago by JamesCherrill: n/a

answer is C. :D
finally i tested..we can invoke static method even it is not in the object.
a constructor's main role is initializing its object.but we also can invoke static methods from the template..

Edited 4 Years Ago by divsok: n/a

ehm ... static doesn't mean it's not in the object.
it just means it has the same value/behaviour for each and every instance of the class, while for instance methods the result of (for instance) a get-method will return the value linked to that instance of the class.

statics data fields and methods are in the Object that we called Class Object to share among the all instances of the class.

Edited 4 Years Ago by divsok: n/a

yes, they are shared through all instances, and if you have a static variable in that class, setting the value for one instance will set it for each instance of this class, so far I agree.

but, the static variables and methods are in the object itself, the values are just not depending on the instance, but on the class they're stored in.
so, for instance, you can create an instance var of a class, which has a static printMe() method, and you can call it as

className.printMe();

or as

var.printMe();

and they'll both perform the exact same action.

Wehn in doubt check the official documentation. Here's what the JLS says:

8.3.1.1 static Fields
If a field is declared static, there exists exactly one incarnation of the field, no
matter how many instances (possibly zero) of the class may eventually be created.
A static field, sometimes called a class variable, is incarnated when the class is
initialized (§12.4).

This makes it clear that static fields cannot be in any object that is an instance of the class. They are associated with the class itself. So although stultuske's explanation correctly describes the end result, Samith Dilhara's version is closer to the JLS.

to stultuske:
when you call var.method() from a reference variable,it doesn't mean the static method is in the same object.Static members are belongs to the class's blueprint(template).actually it is an object that is created by the JVM when you RUN your application. var.method() check the object's methods and if it is not in that object, points to the class Object(template) and finds the method.CLASSNAME.member is the best way to invoke static members.

Edited 4 Years Ago by divsok: n/a

ANS: B

Nope, it is not B.
don't believe me, just write a single class, containing about ten lines of code, test it out and you'll see that ~s.o.s~ was not mistaken in his post, claiming the answer is not B.

This question has already been answered. Start a new discussion instead.