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

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post