A class which is a fundamental component of Object-Oriented Programming in Java.
A class is a blueprint that defines the variables and the methods common to all objects of a certain kind. Simply, a blueprint for creating objects.
- Only one public class is allowed in one source file
What is an objects is in Object-Oriented Programming?
Objects have two major characteristics, the state, and behavior. A programming language object stores its state in fields(variables) and executes their behavior with methods.
In Java, When we create a new project, it creates a new class for us, and it calls the class Main public class Main{}
, inside the Main class, it also creates the method main public static void main(String[] args){}
.
sample/Main.java
package sample;
public class Main {
public static void main(String[] args) {
}
}
Let’s Create People Class
sample/People.java
package sample;
public class People {
}
Access modifier: public, private, protected.
- public keyword is an access modifier that we determine what access we want to allow others to have to the class that is created.
- the world public means unrestricted access to the class.
- private keyword that is no other class can access that class.
- protected keyword that allows classes in the same package to access the class.
You have seen variables used inside a method. That is Local variables. In Java you can not access those local variables outside of the method. Also, if your variables inside a block, you can not access outside of the block.
However, classes allow us to create variables that can be seen and are accessible from anywhere within the class that is created. these are known as member variables or fields. when you create a field (member variables) in class, you need to also specify an excess modifier that works the same way as the excess modifier for a class definition.
A general rule, when you define a field(member variables) in class, with the access modifier private, unlike the class where we have gone with public.
So what private means in fields (member variables). That is encapsulation. In java, encapsulation is used to hide the fields and methods.
Let’s Fields in People Class and We build an object that is base on the People.
sample/People.java
package sample;
public class People {
private String name;
private String country;
private String language;
}
- create fields(members variables) in People Class.
sample/Main.java
package sample;
public class Main {
public static void main(String[] args) {
People tim = new People();
People Mike = new People();
}
}
objects (tim, and mike) that is base on the People class. But objects can not access the fields of People class because all the fields are private in the People class. So, we need to create a public method with a parameter to update fields.
sample/People.java
package sample;
public class People {
private String name;
private String country;
private String language;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
Add setName and getName Method to update and get a name field at another class.
sample/Main.java
package sample;
public class Main {
public static void main(String[] args) {
People tim = new People();
People mike = new People();
tim.setName("Tim");
System.out.println("Name: " + tim.getName());
}
}
Now, you can set up object(tim)’s name.
Let’s execute with command line
$ javac sample/*.java
$ java sample.Main
Name: Tim
- at a project root directory
- 1. compile all java files that are Main.java and People.java.
- 2. Execute Main in the named sample package.
Thus, with private fields(member variables) and public methods to update and use fields. That is the whole concept of encapsulation is that we are not allowing people to access the field directly, and we can add some validation when we assign data to fields.
// you can add some validations.
public void setName(String name) {
if (name.toLowerCase().equals("tim")) {
this.name = name;
} else {
this.name = "?";
}
}