Question:
What information you need to provide when calling the non-static method in Java?
smarttmelanie
2009-08-28 11:18:11 UTC
Is it the object variable name, the method name and the parameter?

Can you give a very simple example? (i'm a newb)

Thanks
Three answers:
Panqueque
2009-08-28 12:12:57 UTC
smart, if the method exists in the class from which it is being called, the method can be called directly:



Object result = doSomething(obj1, obj2);



or, the "this" keyword can be used, which refers to the object making the call; "this" indicates the method exists within the same object:



Object result = this.doSomething(obj1, obj2);



when calling an object in another class, the object name needs to be specified:



Object result = foreignObject.doSomething(obj1, obj2);



hope this helps!
Ben
2009-08-28 18:24:02 UTC
The java (and C++, and Python, and a lot of other Object oriented languages) calling convention is object.method(parameters). For instance,

out.println("hello") calls the println method of out with the parameter "hello"
?
2009-08-30 05:36:57 UTC
//pk



In java, static methods are called just by prefixing the class name.

example:



class Student

{

public static int age;



public static void show()

{

System.out.println("this is show");

}



public void display()

{

System.out.println("this is display");

}

}



------------------------

class My

{

public static void main (String[]args)

{

//calling static methods: show() of student class

Student.show(); //calling static method



//accessing static variable age of student class

Student.age=32;

System.out.println(Student.age);



//calling non static method:display() of student class

Student s = new Student();

s.display(); //calling non static method by class instance



}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...