Some Key Words are:-
1.static, 2.final, 3.this, 4.super
-->static keyword in java is used for memory management mainly.
-->we can apply java static keyword with variables, methods, blocks and nested class.
-->static keyword belongs to the class, than instance of the class.
static can be:-
1.Variable (also known as class area)
2.Method (also known as class method)
3.Block
4.Nested class
1(1): static variable:
Student01.java
package static_keyword;
class Student {
int id;
String name;
static String college = "ITS";
Student(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name + " " + college);
}
}// class Student
public class TestStudentVariable {
public static void main(String[] args) {
Student s1 = new Student(111, "Karan");
Student s2 = new Student(222, "Aryan");
s1.display();
s2.display();
}//main()
}//main class
111 Karan ITS
222 Aryan ITS
Counter1.java
package static_keyword;
public class Counter1 {
int count = 0;// will get memory each time when the instance is created
Counter1() {
count++;// incrementing the value of count
System.out.println(count);
}
public static void main(String[] args) {
Counter1 c1 = new Counter1();
Counter1 c2 = new Counter1();
Counter1 c3 = new Counter1();
}
}
1
1
1
Counter2.java
package static_keyword;
public class Counter2 {
static int count = 0;// will get memory only one and retain it's value
public Counter2() {
count++;// incrementing the value of static count
System.out.println(count);
}
public static void main(String[] args) {
Counter2 c1 = new Counter2();
Counter2 c2 = new Counter2();
Counter2 c3 = new Counter2();
}
}
1
2
3
1(2): static method:
Example of static method
static_method_testcls.java
package static_keyword;
class Students {
int rollno;
String name;
static String college = "ITS";
// static method to change the value of static variable:-
static void change() {
college = "BBDIT";
}
// constructor to initialize the variables:-
Students(int i, String n) {
rollno = i;
name = n;
}
// method to display values:-
void display() {
System.out.println(rollno + " " + name + " " + college);
}
}// class Students
public class static_method_testcls {
public static void main(String[] args) {
Students.change();
// creating objects:-
Students s1 = new Students(111, "Karan");
s1.display();
}
}
111 Karan BBDIT
//Java Program to get the cube of a number:-
package static_keyword;
import java.util.Scanner;
public class Calculate {
static int cube(int x) {
return x * x * x;
}
public static void main(String[] args) {
System.out.println("Enter the number:");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
// Passing user value to the cube method:-
int result = Calculate.cube(number);
System.out.println("Cube of the Entered Number is: " + number + " = "
+ result);
}
}
Enter the number:
5
Cube of the Entered Number is: 5 = 125
1(3): static block:
A3.java
package static_keyword;
public class A3 {
static {
System.out.println("static block is invoked...");
}
public static void main(String[] args) {
System.out.println("Hello main...");
}
}// class A3
static block is invoked...
Hello main...
2.final keyword:-
2.final keyword:-
-->final keyword in java is used to restrict the user.
-->final keyword can be used in many context.
final can be:-
1.Variable
2.Method
3.class
2(1): final variable:
Bike.java
//Java final variable:-
package final_keyword;
public class Bike {
final int speedlimit = 90;// final variable
void run() {
//cannot change the value of final variable:-
speedlimit = 900;
}
public static void main(String[] args) {
Bike obj = new Bike();
obj.run();
}
}// class Bike
/* Compile Time Error */
2(2): final method:
Example of final method:-
Honda1.java
//Java final method:-
package final_keyword;
class Bike01 {
final void run() {
System.out.println("running...");
}
}
class Honda01 extends Bike01 {
//final method cannot override:-
void run() {
System.out.println("running safely with 100 kmph");
}
}
public class Honda1 {
public static void main(String[] args) {
Honda01 h01=new Honda01();
h01.run();
}
}
/*Compile Time Error*/
Example of final class:
Honda2.java
//Java final class:-
package final_keyword;
final class Bike02 {
// empty structure
}// class Bike02
// final class cannot extend:-
class Honda02 extends Bike02 {
void run() {
System.out.println("running safely with 100 kmph");
}
}// class Honda02
public class Honda2 {
public static void main(String[] args) {
Honda02 h02 = new Honda02();
h02.run();
}
}
/* Compile Time Error */
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The type Honda02 cannot subclass the final class Bike02
at final_keyword.Honda02.<init>(Honda2.java:9)
at final_keyword.Honda2.main(Honda2.java:17)
-->final keyword can be used in many context.
final can be:-
1.Variable
2.Method
3.class
2(1): final variable:
Bike.java
//Java final variable:-
package final_keyword;
public class Bike {
final int speedlimit = 90;// final variable
void run() {
//cannot change the value of final variable:-
speedlimit = 900;
}
public static void main(String[] args) {
Bike obj = new Bike();
obj.run();
}
}// class Bike
/* Compile Time Error */
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field Bike.speedlimit cannot be assigned
at final_keyword.Bike.run(Bike.java:9)
at final_keyword.Bike.main(Bike.java:14)
2(2): final method:
Example of final method:-
Honda1.java
//Java final method:-
package final_keyword;
class Bike01 {
final void run() {
System.out.println("running...");
}
}
class Honda01 extends Bike01 {
//final method cannot override:-
void run() {
System.out.println("running safely with 100 kmph");
}
}
public class Honda1 {
public static void main(String[] args) {
Honda01 h01=new Honda01();
h01.run();
}
}
/*Compile Time Error*/
Exception in thread "main" java.lang.VerifyError: class final_keyword.Honda01 overrides final method run.()V
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at final_keyword.Honda1.main(Honda1.java:19)
2(3): final class:
Example of final class:
Honda2.java
//Java final class:-
package final_keyword;
final class Bike02 {
// empty structure
}// class Bike02
// final class cannot extend:-
class Honda02 extends Bike02 {
void run() {
System.out.println("running safely with 100 kmph");
}
}// class Honda02
public class Honda2 {
public static void main(String[] args) {
Honda02 h02 = new Honda02();
h02.run();
}
}
/* Compile Time Error */
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The type Honda02 cannot subclass the final class Bike02
at final_keyword.Honda02.<init>(Honda2.java:9)
at final_keyword.Honda2.main(Honda2.java:17)
some context here.........................
3.this keyword:-
-->this is a reference variable, that refers to the current object.
(1).Method Overloading: changing no.of .arguments:-
TestOverloading1.java
package this_keyword;
class Adder {
static int add(int a, int b) {
return a + b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}//class Adder
public class TestOverloading1 {
public static void main(String[] args) {
System.out.println(Adder.add(11, 11));
System.out.println(Adder.add(11, 11, 11));
}
}
Method overriding in java:-
-->if sub-class has the same method as declared in the parent class, it is known as method overriding in java.
-->this is a reference variable, that refers to the current object.
(1).Method Overloading: changing no.of .arguments:-
TestOverloading1.java
package this_keyword;
class Adder {
static int add(int a, int b) {
return a + b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}//class Adder
public class TestOverloading1 {
public static void main(String[] args) {
System.out.println(Adder.add(11, 11));
System.out.println(Adder.add(11, 11, 11));
}
}
22
33
(2).Method overloading: changing data type of arguments:-
TestOverloading2.java
package this_keyword;
class Adder1 {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
}// class Adder1
public class TestOverloading2 {
public static void main(String[] args) {
System.out.println(Adder1.add(11, 11));
System.out.println(Adder1.add(12.3, 12.6));
}
}
22
24.9
some context here......................................
Method overriding in java:-
-->if sub-class has the same method as declared in the parent class, it is known as method overriding in java.
some context here......................................
4.super keyword:-
-->super keyword in java is a reference variable, which is used to refer immediate parent class object.
-->when ever you create instance of sub-class, an instance of parent class is created implicitly.
super can be:-
1.refer parent class instance variable.
2.used to invoke parent class method.
3.used to invoke parent class constructor.
1.refer parent class instance variable:-
-->when ever you create instance of sub-class, an instance of parent class is created implicitly.
super can be:-
1.refer parent class instance variable.
2.used to invoke parent class method.
3.used to invoke parent class constructor.
1.refer parent class instance variable:-
TestSuper1.java
package super_keyword;
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "black";
void printColor() {
System.out.println(color + ": color of Dog class this is sub class");// prints color of Dog
// class
System.out.println(super.color+": color of Animal class this is super class");// prints color of Animal class
}
}
public class TestSuper1 {
public static void main(String[] args) {
Dog d = new Dog();
d.printColor();
}
}
black: color of Dog class this is sub class
white: color of Animal class this is super class
2.used to invoke parent class method:-
TestSuper2.java
package super_keyword;
class Animal01 {
void eat() {
System.out.println("eating...");
}
}//class Animal01
class DogClass extends Animal01 {
void eat() {
System.out.println("eating bread...");
}
void bark() {
System.out.println("barking...");
}
void work() {
super.eat();
bark();
}
}
public class TestSuper2 {
public static void main(String[] args) {
DogClass d=new DogClass();
d.work();
}
}
eating...
barking...
3.used to invoke parent class constructor:-
TestSuper3.java
package super_keyword;
class Animal02 {
public Animal02() {
System.out.println("animal is created...");
}
}// class Animal02
class DogClasss extends Animal02 {
public DogClasss() {
super();
System.out.println("dog is created...");
}
}// class DogClasss
public class TestSuper3 {
public static void main(String[] args) {
DogClasss d = new DogClasss();
}
}
animal is created...
dog is created...
No comments:
Post a Comment