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

Java Keywords: static, final, super, this Explained

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)
37 views4 pages

Java Keywords: static, final, super, this Explained

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

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

[Link]("Car has " + numberOfWheels + " wheels.");

public class Main {

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

[Link](); // 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

[Link]("Speed limit is " + speedLimit);

public class Main {

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

Bike bike = new Bike();

[Link]();

// [Link] = 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() {

[Link]("Animal makes a sound");

}
class Dog extends Animal {

void sound() {

[Link](); // Calling parent class method

[Link]("Dog barks");

public class Main {

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

Dog dog = new Dog();

[Link]();

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) {


[Link] = id; // 'this' refers to the current object's id

[Link] = name; // 'this' refers to the current object's name

void display() {

[Link]("ID: " + [Link] + ", Name: " + [Link]);

public class Main {

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

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

[Link]();

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