Class in Java

A class is an group of fields, methods, constructors, etc. In any program, class is the first thing that appears. It contains everything to be executed inside it. We must have at least one class in our java program.

We have class keyword to create a class.
Syntax:

class <className> {
    statements
}
If you remember, we are using class since begining.


Class Instances

Whatever is inside class are called class instances. We can have field, method or constructors.

Lets see an example of a class where we have a field (variable) inside our class. It is also called instance variable.

class TestClass { int x; int y; }

Lets see an another example where we also have a method inside our class.

class TestClass { int x; void main() { int y; } }

Also remember our class can have multiple methods inside our class.

class TestClass { void main() { int x; } void second() { int y; } }

We will learn more about methods in next chapter.


Concept of Object Oriented Programming in Java Previous Next Methods in Java