0% found this document useful (0 votes)
16 views4 pages

Keywords Java

keywords in java

Uploaded by

greekcloud1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Keywords Java

keywords in java

Uploaded by

greekcloud1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here’s a simple explana on of the keywords sta c, final, super, and this in Java, with easy examples.

1. sta c Keyword

 Used for class-level variables and methods.

 Sta c variables or methods belong to the class and not to any specific instance (object) of
the class.

Example:

java

Copy code

class Car {

sta c int numberOfWheels = 4; // Sta c variable

sta c void showWheels() { // Sta c method

System.out.println("Car has " + numberOfWheels + " wheels.");

public class Main {

public sta c void main(String[] args) {

Car.showWheels(); // Accessing sta c method without crea ng an object

Output:

Copy code

Car has 4 wheels.

2. final Keyword

 Can be applied to variables, methods, or classes.

 Final variable: its value cannot be changed (like a constant).

 Final method: cannot be overridden by subclasses.

 Final class: cannot be extended (subclassed).

Example:

java
Copy code

class Bike {

final int speedLimit = 90; // Final variable

final void showSpeedLimit() { // Final method

System.out.println("Speed limit is " + speedLimit);

public class Main {

public sta c void main(String[] args) {

Bike bike = new Bike();

bike.showSpeedLimit();

// bike.speedLimit = 100; // This will cause an error because speedLimit is final

Output:

bash

Copy code

Speed limit is 90

3. super Keyword

 Used to refer to the parent class (superclass) of a subclass.

 Can be used to call parent class methods or constructors.

Example:

java

Copy code

class Animal {

void sound() {

System.out.println("Animal makes a sound");

}
class Dog extends Animal {

void sound() {

super.sound(); // Calling parent class method

System.out.println("Dog barks");

public class Main {

public sta c void main(String[] args) {

Dog dog = new Dog();

dog.sound();

Output:

css

Copy code

Animal makes a sound

Dog barks

4. this Keyword

 Refers to the current instance of the class.

 Used to resolve conflicts between class a ributes and parameters with the same name or to
call other constructors.

Example:

java

Copy code

class Student {

int id;

String name;

Student(int id, String name) {


this.id = id; // 'this' refers to the current object's id

this.name = name; // 'this' refers to the current object's name

void display() {

System.out.println("ID: " + this.id + ", Name: " + this.name);

public class Main {

public sta c void main(String[] args) {

Student student = new Student(101, "John");

student.display();

Output:

yaml

Copy code

ID: 101, Name: John

These keywords help manage object-oriented concepts like inheritance, constant values, and class-
level behavior.

You might also like