×

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

Related Topics

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

&& Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.