Method

Java’s methods define what an object can do. In Java programming, method names usually start with a lowercase letter.

[AccessLimiter] [static/final/abstract/synchronized] ReturnValue MethodName([Parameters]) {
	... //Local variable declaration and methods
}

Java can also add access limiter to methods. Like member variables, you can restrict access using public, private, and protected.

class Fruit {
	int apple;
	int straw;
	int grapes;
	int sum;

	Fruit(int apple, int straw, int grapes) {
		this.apple = apple;
		this.straw = straw;
		this.grapes = grapes;
	}
	public int count() {
		sum = apple + straw + grapes;
		return sum;
	}
}

class MethodDemo1 {
	public static void main(String args[]) {
		int total;
		Fruit f1 = new Fruit(30, 30, 30);
		total = f1.count();
		System.out.println("The total number of objects f1 = " + total);
		//Access to object attribute variables to read the number of apples
		//The value of the object property variable can be directly changed (f1.apple = 90), and the information can be modulated.
		System.out.println("Number of apples in object f1 = " + f1.apple);
		System.out.println("Number of straws in object f1 = " + f1.straw);
		System.out.println("Number of grapes in object f1 = " + f1.grapes);
	}
}
Java Method Basic Example 1
//Example of encapsulation
class Fruit {
	private int a;
	private int s;
	private int g;
	private int sum;

	Fruit(int apple, int straw, int grapes) {
		a = apple;
		s = straw;
		g = grapes;
		this.count();
	}
	private void count() {
		sum = a + s + g;
	}
	public int gettotal() {
		//The sum of all fruits is accessed only through this method
		return sum;
	}
	public int getapple() {
		//Method to allow read only on value 'a'
		return a;
	}
	public int getstraw() {
		return s;
	}
	public int getgrapes() {
		return g;
	}
}

class MethodDemo2 {
	public static void main(String args[]) {
		int total;
		Fruit f1 = new Fruit(30, 30, 30);
		total = f1.gettotal(); //If f1.count () is used, an error occurs
		System.out.println("The total number of objects f1 = " + total);
		//You can only read the number of apples using the method
		//If the user directly accesses an object attribute variable with a statement such as "f1.a = 50", an error occurs
		System.out.println("Number of apples in object f1 = " + f1.getapple());
		System.out.println("Number of straws in object f1 = " + f1.getstraw());
		System.out.println("Number of grapes in object f1 = " + f1.getgrapes());
	}
}
Java Method Basic Example 2

Class method: static

Java can declare class methods with static. Class methods, like class variables, can be accessed through class names. If you declare a class method, you can use only class variables in the class method.

class Box {
	int width;
	int height;
	int depth;
	long idNum;
	static long boxID = 100;
	//Class methods use only class variables
	static long getcurrentID() {
		return boxID++;
	}
}

class StaticMethodDemo {
	public static void main(String args[]) {
		Box mybox1 = new Box();
		mybox1.idNum = Box.getcurrentID();
		Box mybox2 = new Box();
		mybox2.idNum = Box.getcurrentID();
		System.out.println("id number of mybox1 : " + mybox1.idNum);
		System.out.println("id number of mybox2 : " + mybox2.idNum);
		System.out.println("The number in the next box is " + Box.boxID);
	}
}
Class method basic example 1
class One {
	int value;
	public One() {
		this(100);
	}
	public One(int value) {
		this.value = value;
		Another.methodA(this);
	}
}

class Another {
	static void methodA(One ins) {
		System.out.println("The value in method A : " + ins.value);
	}
}

class OneTest {
	public static void main(String args[]) {
		One t1 = new One();
		System.out.println("Default Value : " + t1.value);
		int value = Integer.parseInt(args[0]);
		Ont t2 = new One(value);
		System.out.println("User entered value : " + t2.value) ;
	}
}
Class method basic example 2

final, abstract, synchronized method

In Java, methods can be declared as final, abstract, and synchronized, which have the following meanings:

  • final: Methods declared final can not be overrided by subclasses
  • abstract: can be declared within an abstract class as an abstract method
  • synchronized: method for synchronizing threads

Method access

Java has access to general and class methods.

ClassName.ClassMethodName(parameter)
Class Method Access Formats
ObjectName.ObjectMethodName(parameter)
Generic Method Access Formats

Method return value

In Java, a method must have a return value specified in its declaration. If there is no return value, it is specified as void, and data can be returned from the reference data type as well as the basic data type.

//Methods returning basic datatype data must match the datatype returned by the return type specified in the method declaration
public int sum(int a, int b) {
	int c;
	c = a + b;
	return c;
}
//Methods that return an object that is a reference type
public Box volume_compute(Box instance_box) {
	Box v_box = new Box();
	v_box.width = instance_box.width;
	v_box.height = instance_box.height;
	v_box.depth = instance_box.depth;
	v_box.volume = v_box.width * v_box.height * v_box.depth;
	return v_box;
}
Method return value Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of