×

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

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.