answersLogoWhite

0


Best Answer

If the method is static you can do this way:

Classname.method()

If the method is not static then you would have to instantiate the class that contains this method and then call it using that object.

Classname obj = new Classname();

obj.method()

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Details may vary depending on the programming language; but usually you type the name of the method, and a pair of parentheses. If parameters are needed, type them within the parentheses, and separated by commas. For example:

myMethod();

myMethod(a, b);

If the method is in another class, you usually need to specify either an object...

myObject.myMethod();

or the class name. Here is an example from Java, using the Math() class:

Math.pow(2.0, 10.0);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you call a method from another class in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can an object have several different classes?

Inheriting multiple base classes is not allowed in Java. If you are curious asking me why doesn't java allow multiple inheritance, think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature.


What does import javax.swing.jaframe mean?

Its a Java programming language call to include a method called Jaframe which is is the Javax.Swing class Library.


Can you refer non static method from static content in java?

Shortly, you can not.Different approaches are however available.1. Put your non static method in different class. Then call it from your static content by first instantiating the class.2. Make a duplicate static method for your non static method and use from your static content.


Can you call an abstract method from a non abstract method in java?

We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}


What is function overriding in Java?

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.

Related questions

How do you call main class with in main class in java?

We can't call a class. We always call a method in java.


What is method call in java?

Calling a method in Java is when you run code associated with a specific class, using the name of an instance object of a class, followed by the dot operator, followed by the name of the method, followed by the arguments of the method, enclosed in parentheses.


How do you call a method in Java?

It's when you take a object and run some code in the class of that object.


How do you call a method from inside the main method?

I assume the question is about Java. How you call a method from inside the main method depends on whether this is an instance method or a static method. To call an instance method, you need to have a valid instance. Say foo is an instance of class Foo, which was created inside the main method. Then to call the instance method implode() use foo.implode() To call the static method bar() of class Foo, use Foo.bar().


Is it possible to override the main method in Java?

Theoretically yes. You will use only one main method for your whole java application and hence such a situation will not come up in real life. Imagine: You have a main in class A and one more in class B which extends class A. When you try to run the class B, only that main method will be executed and not the main in class A. So this override is irrelevant and we cannot call class A's main method at all


Why multiple inheritance is not used in java?

If you are curious asking me why doesn't java allow multiple inheritance, think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature.


Can an object have several different classes?

Inheriting multiple base classes is not allowed in Java. If you are curious asking me why doesn't java allow multiple inheritance, think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature.


What does import javax.swing.jaframe mean?

Its a Java programming language call to include a method called Jaframe which is is the Javax.Swing class Library.


Why multiple inheritance not in java?

think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature.


What are the inconsistencies of using multiple inheritace in java?

If you are curious asking me why doesn't java allow multiple inheritance, think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature.


Why java doesn't support multiple inheritance and pointers concept?

think about this scenario. Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say you create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time you need to call the drive() method, what would happen? Your JVM wouldn't know which method to invoke and you may have to instantiate one of the classes that you already inherit in order to call its appropriate method. Sounds confusing right? To avoid this nonsense is why the creators of java did not include this direct multiple inheritance feature. Similarly, Pointers is where most C programmers make catastrophic errors and the creators of Java thought that the job of messing with memory locations is better left to the experts and hence there is no pointers in java


How you compare and contrast overloading and overriding methods in java?

Method overloading is when you have multiple methods in a class that have the same name but a different signature. Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.