Java class structure and class declaration

Structure of the Java class

A class is a template that creates an object, and a Java program is just a collection of these classes.

In Java, a class consists of two elements, each of which is shown below.

First, the data attributes that an object can have include the data itself.

Second, methods can manipulate data.

The code structure of the Java class is shown below.

class ClassName { //Class header area

	//Member variable
	type1 varName1 = value1;
	typeN varNameN = valueN;

	//Constructor
	ClassName(args1) {
		//Write the initialization process of the generated object
	}
	ClassName(argsN) {
		...
	}

	//Method
	mtype mName1(margs1) {
		//Writing methods
	}
	mtype mNameN(margsN) {
		...
	}

}
class SampleClass {
	int a;
	int b;
	int c;

	//constructor. Name is the same as class name
	public SampleClass(int x, int y, int z) {
		a= x;
		b=y;
		c=z;
	}

	// Method
	public int sum() {
		int d;
		d = a + b + c;
		return d;
	}
}
Java class basic example

Java class declaration

When you create a class in Java, you use a qualifier that specifies the nature of the class.

[public/final/abstract] class ClassName {  //Class header
	... //Class Members
}

In the code above, the qualifiers are public, final, abstract, etc. Each qualifier has the following characteristics.

  • public: This class can be used by other classes. That is, all classes can create objects of the class specified by this qualifier.
  • final: A class that can not have subclasses.
  • abstract: An abstract class with abstract methods. An abstract method is a bodyless method that describes the actual function.

If you do not specify qualifiers such as public, final, and abstract, you can only access classes in the same package.

//Only the attributes of the data
public class Box {
	int width;
	int height;
	int depth;
}
Class declaration example 1
//Classes with properties and methods of data
public class Box {
	int width;
	int height;
	int depth;
	public void volume() { //Method
		int vol;
		vol = width * height * depth;
		System.out.println("volumn is" + vol);
	}
}
Class declaration example 2
//Classes with properties, constructors, and methods of data
class Box {
	int width;
	int height;
	int depth;
	public void Box(int w, int h, int d) {
		width = w;
		height = h;
		depth = d;
	}

	public void volume() {
		int vol;
		vol = width * height * depth;
		System.out.println("Volume is " + vol);
	}
}
Class declaration example 3

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