[Go to site: main page, start]

0% found this document useful (0 votes)
19 views10 pages

Java Class Methods Explained

The document provides an overview of Java class methods, including their declaration, access, and the use of the 'this' keyword. It explains the differences between public and static methods and introduces the finalize() method for object cleanup before garbage collection. Examples are provided to illustrate the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Java Class Methods Explained

The document provides an overview of Java class methods, including their declaration, access, and the use of the 'this' keyword. It explains the differences between public and static methods and introduces the finalize() method for object cleanup before garbage collection. Examples are provided to illustrate the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Page 1 of 10

Home Whiteboard Online Compilers Practice Articles Tools

Java - Class Methods

Java Class Methods


The class methods are methods that are declared within a class. They perform specific
operations and can access, modify the class attributes.

Advertisement

Creating (Declaring) Java Class Methods


Class methods declaration is similar to the user-defined methods declaration except that
class methods are declared within a class.

The class methods are declared by specifying the access modifier followed by the return
type, method_name, and parameters list.

Syntax

Use the below syntax to declare a Java class method:

[Link] w w .[Link]/java/java_class_methods.htm 1/10


Page 2 of 10

public class class_name {


modifier returnType nameOfMethod(Parameter List) {
// method body
}
}

The syntax shown above includes −

modifier − It defines the access type of the method and it is optional to use.

returnType − The returns data type of the class method.

nameOfMethod − This is the method name. The method signature consists of the
method name and the parameter list.

Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero
parameters.
method body − The method body defines what the method does with the
statements.

Example

Here is the source code of the above defined method called minimum(). This method takes
two parameters n1 and n2 and returns the minimum between the two −

class Util {
/** the snippet returns the minimum between two numbers */
public int minimum(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;

return min;
}
}

Accessing Java Class Methods

[Link] w w .[Link]/java/java_class_methods.htm 2/10


Page 3 of 10

To access a class method (public class method), you need to create an object first, then
by using the object you can access the class method (with the help of dot (.) operator).

Syntax

Use the below syntax to access a Java public class method:

object_name.method_name([parameters]);

Example

Following is the example to demonstrate how to define class method and how to access
it. Here, We've created an object of Util class and call its minimum() method to get
minimum value of given two numbers −

package [Link];

class Util {
public int minimum(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;

return min;
}
}
public class Tester {

public static void main(String[] args) {


int a = 11;
int b = 6;

Util util = new Util();

int c = [Link](a, b);


[Link]("Minimum Value = " + c);

[Link] w w .[Link]/java/java_class_methods.htm 3/10


Page 4 of 10

}
}

Output

Minimum value = 6

this Keyword in Java Class Methods


this is a keyword in Java which is used as a reference to the object of the current class,
with in an instance method or a constructor. Using this you can refer the members of a
class such as constructors, variables and methods.

Note − The keyword this is used only within instance methods or constructors

In general, the keyword this is used to −

Differentiate the instance variables from local variables if they have same names,
within a constructor or a method.

class Student {
int age;
Student(int age) {
[Link] = age;
}
}

Call one type of constructor (parametrized constructor or default) from other in a


class. It is known as explicit constructor invocation.

class Student {
int age
Student() {
this(20);
}

Student(int age) {
[Link] = age;

[Link] w w .[Link]/java/java_class_methods.htm 4/10


Page 5 of 10

}
}

Example: Using this Keyword in Java Class Methods

Here is an example that uses this keyword to access the members of a class. Copy and
paste the following program in a file with the name, [Link].

package [Link];

public class Tester {


// Instance variable num
int num = 10;

Tester() {
[Link]("This is an example program on keyword this");
}

Tester(int num) {
// Invoking the default constructor
this();

// Assigning the local variable num to the instance variable num


[Link] = num;
}

public void greet() {


[Link]("Hi Welcome to Tutorialspoint");
}

public void print() {


// Local variable num
int num = 20;

// Printing the local variable


[Link]("value of local variable num is : "+num);

// Printing the instance variable


[Link]("value of instance variable num is : "+[Link]);

[Link] w w .[Link]/java/java_class_methods.htm 5/10


Page 6 of 10

// Invoking the greet method of a class


[Link]();
}

public static void main(String[] args) {


// Instantiating the class
Tester obj1 = new Tester();

// Invoking the print method


[Link]();

// Passing a new value to the num variable through parametrized


constructor
Tester obj2 = new Tester(30);

// Invoking the print method again


[Link]();
}
}

Output

This is an example program on keyword this


value of local variable num is : 20
value of instance variable num is : 10
Hi Welcome to Tutorialspoint
This is an example program on keyword this
value of local variable num is : 20
value of instance variable num is : 30
Hi Welcome to Tutorialspoint

Public Vs. Static Class Methods


There are two types of class methods public and static class method. The public class
methods are accessed through the objects whereas, the static class methods are
accessed are accesses without an object. You can directly access the static methods.

Example

[Link] w w .[Link]/java/java_class_methods.htm 6/10


Page 7 of 10

The following example demonstrates the difference between public and static class
methods:

public class Main {


// Creating a static method
static void fun1() {
[Link]("fun1: This is a static method.");
}

// Creating a public method


public void fun2() {
[Link]("fun2: This is a public method.");
}

// The main() method


public static void main(String[] args) {
// Accessing static method through the class
fun1();

// Creating an object of the Main class


Main obj = new Main();
// Accessing public method through the object
obj.fun2();
}
}

Output

fun1: This is a static method.


fun2: This is a public method.

The finalize( ) Method


It is possible to define a method that will be called just before an object's final destruction
by the garbage collector. This method is called finalize( ), and it can be used to ensure
that an object terminates cleanly.
Chapters Categories

[Link] w w .[Link]/java/java_class_methods.htm 7/10


Page 8 of 10

For example, you might use finalize( ) to make sure that an open file owned by that
object is closed.

To add a finalizer to a class, you simply define the finalize( ) method. The Java runtime
calls that method whenever it is about to recycle an object of that class.

Inside the finalize( ) method, you will specify those actions that must be performed before
an object is destroyed.

The finalize( ) method has this general form −

protected void finalize( ) {


// finalization code here
}

Here, the keyword protected is a specifier that prevents access to finalize( ) by code
defined outside its class.

This means that you cannot know when or even if finalize( ) will be executed. For
example, if your program ends before garbage collection occurs, finalize( ) will not
execute.

TOP TUTORIALS

Python Tutorial
Java Tutorial
C++ Tutorial

C Programming Tutorial
C# Tutorial
PHP Tutorial

R Tutorial
HTML Tutorial
CSS Tutorial

JavaScript Tutorial
SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial


Amazon Web Services Tutorial

[Link] w w .[Link]/java/java_class_methods.htm 8/10


Page 9 of 10

Microsoft Azure Tutorial

Git Tutorial
Ethical Hacking Tutorial
Docker Tutorial

Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial

SDLC Tutorial
Unix Tutorial

CERTIFICATIONS

Business Analytics Certification


Java & Spring Boot Advanced Certification

Data Science Advanced Certification


Cloud Computing And DevOps
Advanced Certification In Business Analytics

Artificial Intelligence And Machine Learning


DevOps Certification
Game Development Certification

Front-End Developer Certification


AWS Certification Training
Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler


Online Python Compiler
Online Go Compiler

Online C Compiler
Online C++ Compiler
Online C# Compiler

Online PHP Compiler


Online MATLAB Compiler
Online Bash Terminal

Online SQL Compiler


Online Html Editor

[Link] w w .[Link]/java/java_class_methods.htm 9/10


Page 10 of 10

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.

© Copyright 2025. All Rights Reserved.

[Link] w w .[Link]/java/java_class_methods.htm 10/10

You might also like