Java Class Keyword
- We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.
- Java is mainly depending on these two terms that is classes and objects.
- So, in this article we will be learning about class keyword which plays a major role in java programming language.
- The keyword classes are implemented in java programming language to implement the new java class in the program.
- The java class acts as a heading to the block of code. The class contains a block of code that contains field, method, constructor and many more.
- We can also define the class as a group of variables, constructors, methods, blocks etc.
- The objects are created from the class because the class is a template or blueprint which generates the objects.
- We consider it as logical entity and it cannot be appeared physically.
- In java every line of the code or program that is going to be executed or runned must and should be inside a class.
- Every class that is declared should always start with the uppercase for first letter of the class name.
- And class name of the program should be the as a file name or vice versa.
Points to be remembered:
- Occurrence of every class is only due to the class instance.
- Always the name of the class should be not same or unique in a package.
- In classes declaration we can develop the nested classes that is in one class we can contain or declare more than one class.
- In class we can declare only few words with the class such as public, abstract, strictp, final modifier. We can also declare other modifiers such as private, protected and static in the inner class of the java.
- Without the requirement of the instance of that class we can implement the class keyword as Class.class to retrieve the object of the class that is object class.
Syntax for Declaring Class in Java
To refer a class inside or outside a class the class name should be created. The simple syntax for declaring the class is given below:
Syntax:
class Classname
Example
class Raju //Here classname is Raju
{
// declaration of variables and constructors
//method declaration can also be done.
}
Here the keyword is class implemented for defining the class in java. Followed by the class declaration we have the body or block of the class where variables, methods, constructors etc are defined inside the block or body of the class.
Examples of Class Keyword
Example1: Class keyword with simple example.
public class ClassEx1 {
public static void main (String [] args)
{
System.out.println("Raju is good boy");
}
}
Output:
Raju is good boy
Example2: Defining two classes in a single program.
class Student
{
int rollno.
String name.
long fee.
public student (int rollno, String name, long fee) {
super ();
this. rollno = rollno.
this.name = name.
this.fee = fee.
}
}
class ClassEx
{
public static void main (String [] args) {
students=new student(5h5,"David",125000);
System.out.println(s. rollno+" "+s.name+" "+s.fee);
}
}
Output:
5h5David125000
Example3:
class ClassEx {
String cal="class of private will be invoked";
private class raj
{
void display ()
{
System.out.println(cal);
}
}
public static void main (String args []){
ClassEx obj=new ClassEx ();
ClassEx.raj in=obj.new raj ();
in. display ();
}
}
Output:
class of private will be invoked
Example4: General java Class Program
class man1
{
// Instance variables, describes state/properties of object of this class.
String id;
int years.
int size.
// methods, describes behaviors of object of this class.
public void speaks () {
System.out.println("Hi my idis: "+id+", year: "+year
+" size: "+size+" cm. I can Speaks");
}
public void Run () {
System.out.println("Hi my idis: "+id+", year: "+year
+" size: "+size+" cm. I can Run");
}
}
class ClassDe
{
public static void main (String [] args)
{
manm1 = new man (); // Creating object of class Person
m1.id = "David";
m2. year = 2022.
m1. size = 170;
m1. speaks ();
m1.run ();
}
}
We declared the program with the main class name as ClassDemo.java
So that we run the program with the java ClassDemo
Output:
Hi my idis:David, year: 2022,size: 170 cm. I can speak
Hi my idis:David, year: 2022,size: 170 cm. I can run