How to call class in java

Java Guides

Now, in the previous article, I mentioned that you wanted to minimize the scriptlets and declarations in a JSP.

You wanna avoid dumping thousands of lines of code in your JSP. Now, it’s okay to add small bits of script, small bits of declarations, but don’t overdo it.

So, in order to kinda help with this problem, you can refactor your code into a separate Java class or make use of MVC.

So the Java class will have all of our code, all of our business logic, and so on, and the JSP can simply make a call, let the Java code or the Java class do the heavy lifting, and then the JSP can get the results and continue on with its processing.

Development Steps

  1. Create a Java class
  2. Create a JSP page
  3. Call Java methods from Java class into JSP page.

1. Create a Java class — Calculator.java

I assume that you have already JSP application. Now you can create a Java class named Calculator.java and keep following code into it:

package net.javaguides.jsp.tutorial; public class Calculator < public int addition(int num1, int num2) < return (num1 + num2); > public int substraction(int num1, int num2) < return (num1 - num2); > public int multiplication(int num1, int num2) < return (num1 * num2); > public int division(int num1, int num2) < return (num1 / num2); > >

Create a JSP page — calculator.jsp

page import="net.javaguides.jsp.tutorial.Calculator"%>

Here is a complete example which is calling Calculator Java class methods and displaying results on brower.

page import="net.javaguides.jsp.tutorial.Calculator"%> page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> html> head> meta charset="ISO-8859-1"> title>Insert title heretitle> head> body>  Calculator calculator = new Calculator(); %> Addition of 20 + 10 =  calculator.addition(20, 10) %> br>br> Subtraction of 20 - 10 =  calculator.substraction(20, 10) %> br>br> Multiplication of 20 * 10 =  calculator.multiplication(20, 10) %> br>br> Division of 20/10 =  calculator.division(20, 10) %> body> html>

Источник

How to call a method from another Class Java

In Java, methods/functions are nothing but a set of instructions or a block of code that will come into action when someone calls it. A method can have different instructions that work combinedly to perform a specific task. The code specified within the method will get executed only when someone calls it. In Java, methods are of two types i.e. user-defined and predefined methods.

In Java, a method can be invoked within the same class as well as from some other java class. Any method regardless of its type i.e. predefined or user-defined will be invoked/called using the dot syntax.

This post will present an in-depth overview of how to invoke a java method from another class with the help of examples. So, let’s get started!

Invoking a Java method from another class

We have to create the object of a class(class to be invoked) to invoke a method of one class in some other java class.

Let’s consider an example to understand how to invoke a method from another Java class:

  • Let’s say we have two classes i.e. “FirstClass” and “SecondClass”.
  • We assume that the “FirstClass” has a method named “Hello()” and we have to invoke it in the “SecondClass”.
  • To do that, first, we need to create an object of “FirstClass” in the main method of the “SecondClass”.
  • Once an object of the “FirstClass” is created, then we can invoke any method or attribute of the “FirstClass” within the “SecondClass” using that object.

Calling a public method from another class

We all know that programming languages have some access modifiers that define the scope/accessibility of a method, constructor, or class. “public” is one of them that is accessible inside as well as outside of a class/package.

Example: invoke a public method from some other class
In this program, we will create two classes “FirstClass” and “SecondClass” as shown in the below-given code blocks:

class FirstClass {
public void printMessage ( ) {
System. out . println ( «Welcome to linuxhint.com» ) ;
}
}

In the “FirstClass”, we created a method named “printMessage()” which will show a message “welcome to linuxhint.com” whenever someone invokes it.

SecondClass

public class SecondClass {
public static void main ( String [ ] args ) {
FirstClass classObj = new FirstClass ( ) ;
classObj. printMessage ( ) ;
}
}

The “SecondClass” served the below-listed functionalities:

  • Created an object of the “FirstClass” using a new Keyword.
  • Invoked the “printMessage()” method using the object of the “FirstClass”.

The output proved that the “printMessage()” method of the “FirstClass” was successfully invoked from the “SecondClass”.

Calling a protected method from another Java class

In java, if a method of a class is declared with the “protected” keyword, then it can be accessed by any other class of the same package. A method declared with the protected keyword can’t be accessed out of the package directly. However, it can be accessed outside the package with the help of inheritance.

Example: how to invoke a protected method from some other class of the same package
In the following program, we will create two classes “FirstClass” and “SecondClass”:

class FirstClass {
protected void printDomainName ( ) {
System. out . println ( «Linuxhint.com» ) ;
}
}

Within FirstClass, we created a method named “printDomainName()” with the protected access modifier.

SecondClass:

Within the second class, firstly, we created an object of the “SecondClass”. Afterwards, we utilized that object to invoke the “printDomainName()” method of the FirstClass.

The above snippet verifies that we can call the protected method from some other class of the same package.

Calling a static method from another class

In Java, there is no need to create the object of a class while working with the static methods. A static method of one class can be invoked from some other class using the class name.

Example: How to invoke a static method from another class?

class FirstClass {
static void printDomain ( ) {
System. out . println ( «this is linuxhint.com» ) ;
}
}

public class SecondClass {
public static void main ( String [ ] args ) {
FirstClass. printDomain ( ) ;
}
}

In this example program, we created two classes “FirstClass” and “SecondClass”. We invoked the static method of the “FirstClass” from the main method of the “SecondClass”. Consequently, we will get the following output:

The output verified that the static method of one class can be accessed/invoked from another class directly with the class name.

Conclusion

In Java, a method can be invoked from another class based on its access modifier. For example, a method created with a public modifier can be called from inside as well as outside of a class/package. The protected method can be invoked from another class using inheritance. A static method of one class can be invoked from some other class using the class name. This write-up considered multiple examples to explain how to call a method from another class in Java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.

Источник

Call a Method in Another Class in Java

Call a Method in Another Class in Java

  1. Call a Method in Another Class in Java
  2. Call a static Method in Another Class in Java
  3. Call a protected Method in Another Class in Java
  4. Call a public Method in Another Class in Java

This tutorial introduces how to call a method of another class in Java.

In Java, a class can have many methods, and while creating applications, we can call these methods into the same class and another class. There can be several scenarios where a method can be called in another class. So, let’s start with examples.

Call a Method in Another Class in Java

To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName() . We access this method from the second class SimpleTesting by using the object of the Student class. See the example below.

class Student  String name;   Student(String name)  this.name = name;  >  public String getName()   return this.name;  > >  public class SimpleTesting   public static void main(String[] args)   Student student = new Student("John");  String name = student.getName();  System.out.println("Student name is : "+name);  > > 

Call a static Method in Another Class in Java

It is another scenario where we are calling a static method of another class. In the case of a static method, we don’t need to create an object to call the method. We can call the static method by using the class name as we did in this example to call the getName() static method. See the example below.

class Student  static String name;   static String getName()   return name;  > > public class SimpleTesting   public static void main(String[] args)   Student.name = "John";  String name = Student.getName();  System.out.println("Student name is : "+name);  > > 

Call a protected Method in Another Class in Java

If the instance method of a class is declared as protected , it can be called only inside the subclass. Here, we extend the Student class into the SimpleTesting class and call the getName() method using the object of SimpleTesting class. See the example below.

class Student  protected String name;   protected String getName()   return this.name;  > > public class SimpleTesting extends Student  public static void main(String[] args)   SimpleTesting st = new SimpleTesting();  st.name = "John";  String name = st.getName();  System.out.println("Student name is : "+name);  > > 

We can not call private methods of any class into another class since private methods are only limited to the same class.

Call a public Method in Another Class in Java

A method declared as the public is available for outside access and can be called into another class. Here, we called a public method getName() into another class by using the object of Student class. See the example below.

class Student  public String name;   public String getName()   return this.name;  > > public class SimpleTesting  public static void main(String[] args)   Student st = new Student();  st.name = "John";  String name = st.getName();  System.out.println("Student name is : "+name);  > > 

Related Article — Java Method

Источник

Читайте также:  Java save bufferedimage as file
Оцените статью