Skip to content

Commit 1bb093c

Browse files
authored
Add files via upload
1 parent 7a7723b commit 1bb093c

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed

ConstVsStatic.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package Java_Technical_Training;
2+
3+
/*
4+
* static block vs init block
5+
* static block vs init block vs constructor
6+
* if static is used in both parent and child class, the static block of parent class will run first
7+
*/
8+
9+
class Q {
10+
static int a; // ! Eager Binding, Eager Loading
11+
int b; // ! instance variable, during object creation, lazy loading
12+
13+
Q() {
14+
a = 20;
15+
System.out.println("priority 4: This is constructor ");
16+
b = 20;
17+
}
18+
19+
{
20+
a = 10;
21+
System.out.println("priority 3: This is init block " + a);
22+
23+
}
24+
// init is pre constructor call
25+
// initialize instance variable, same work as constructor
26+
// ! it ruins the readiablity of code
27+
28+
static {
29+
a = 30;
30+
System.out.println("priority 1: Static Block will execute during class loading : " + a);
31+
// used to initialize static variables
32+
// pre logic or preprocessing before initilization
33+
}
34+
static {
35+
a = 40;
36+
System.out.println("priority 2: This is second static block " + a);
37+
}
38+
39+
static void show() {
40+
System.out.println("A is : " + a);
41+
}
42+
}
43+
44+
class Parents {
45+
static int x;
46+
static {
47+
x = 10;
48+
System.out.println("this is static from parents class : " + x);
49+
}
50+
static {
51+
System.out.println("this is second static block from parents class");
52+
}
53+
{
54+
System.out.println("I am the parent init block");
55+
}// inti block
56+
57+
Parents() {
58+
System.out.println("I am parents class default constructor");
59+
}// constructor
60+
61+
}
62+
63+
class Child extends Parents {
64+
static int y;
65+
66+
static {
67+
y = 0 + 10;
68+
System.out.println("this is static from child class " + y);
69+
}
70+
}
71+
72+
public class ConstVsStatic {
73+
public static void main(String[] args) {
74+
// Q.show();
75+
// Q obj = new Q();
76+
Child obj2 = new Child();
77+
78+
}
79+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package Java_Technical_Training;
2+
3+
/*
4+
* Method overloading and type promotion
5+
*
6+
*
7+
* type promotion
8+
*
9+
* byte -> short -> int -> long -> double
10+
* long ->float-> double
11+
* int -> float -> double
12+
* int -> double
13+
*/
14+
15+
class TypePromotion {
16+
void show(byte x) {
17+
System.out.println(x + " : Byte ----");
18+
}
19+
20+
void show(short x) {
21+
System.out.println(x + " : Short ----");
22+
}
23+
24+
void show(String x) {
25+
System.out.println(x + " : String ----");
26+
}
27+
28+
void show(int x) {
29+
System.out.println(x + " : Int ----");
30+
}
31+
32+
void show(char x) {
33+
System.out.println(x + " : Char ----");
34+
}
35+
36+
void show(float x) {
37+
System.out.println(x + " : float ----");
38+
}
39+
40+
void show(int... x) {
41+
System.out.println(x + " : Var args ----");
42+
}
43+
44+
// ! first chance given to primitive type, than it goes for var args then it
45+
// ! goes for Wrapper classes, then it shows error
46+
47+
void show(Integer x) {
48+
System.out.println(x + " : Integer class ----");
49+
}
50+
51+
}
52+
53+
class Shopping {
54+
void search(double amount) {
55+
System.out.println("Product is available");
56+
}
57+
58+
void search(String name) {
59+
System.out.println("Product is available");
60+
61+
}
62+
63+
void search(String name, double amount) {
64+
System.out.println("Product is unavailable");
65+
66+
}
67+
68+
void search(double amount, String name) {
69+
System.out.println("Product is unavailable");
70+
71+
}
72+
73+
}
74+
75+
public class MethodOverloadingAndTypePromotion {
76+
public static void main(String[] args) {
77+
Shopping obj = new Shopping();
78+
obj.search("Apple");
79+
obj.search(80000.0);
80+
obj.search("Watch", 75000.0);
81+
obj.search(7500000.0, "Car");
82+
83+
TypePromotion obj2 = new TypePromotion();
84+
obj2.show('A');
85+
obj2.show("ankit");
86+
obj2.show(34);
87+
obj2.show('A' + 10);// will converted to int
88+
obj2.show(34.5f);
89+
obj2.show(343443L);
90+
obj2.show(343443454L);
91+
obj2.show();
92+
obj2.show(10, 59);
93+
obj2.show(4543);
94+
95+
}
96+
}

StaticOverriding.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package Java_Technical_Training;
2+
3+
/*
4+
* can we override static method : no, as it is bind with class not objects
5+
*
6+
* static - keyword, they are loaded once, bind with class name
7+
* static methods can be called without class objects
8+
* static is also present for objects
9+
*
10+
*
11+
12+
}
13+
*/
14+
15+
class StaticExample {
16+
int count; // instance variable
17+
static int counter; // class variable
18+
19+
StaticExample() {
20+
count++; // ! instance variable, object bind ( runtime binding)
21+
counter++; // ! class variable, class bind, static bind
22+
System.out.println("Intance var count is : " + count + " Static var counter is : " + counter);
23+
}
24+
25+
static void show() {
26+
System.out.println("I am the show : ");
27+
}
28+
}
29+
30+
class ParentClass {
31+
void disp() {
32+
System.out.println("I am the parent class display ");
33+
}
34+
}
35+
36+
class ChildClass extends ParentClass {
37+
void disp() {
38+
System.out.println("I am the child class display ");
39+
}
40+
41+
static void print() {
42+
System.out.println("I am static method of child class ");
43+
}
44+
}
45+
/*
46+
* overriding :
47+
* same signature as per parents is essential
48+
* inheritance must be present
49+
* depends on object creation
50+
*/
51+
52+
public class StaticOverriding {
53+
public static void main(String[] args) {
54+
StaticExample obj1 = new StaticExample();
55+
StaticExample obj2 = new StaticExample();
56+
StaticExample obj3 = new StaticExample();
57+
StaticExample obj4 = new StaticExample();
58+
StaticExample.show();
59+
60+
// overriding
61+
ChildClass obj5 = new ChildClass();
62+
obj5.disp();
63+
obj5.print();
64+
ChildClass.print();
65+
66+
ParentClass obj6 = new ChildClass();
67+
obj6.disp(); //
68+
// obj6.print();
69+
70+
}
71+
}

StaticVsInstance.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Java_Technical_Training;
2+
3+
class Demo {
4+
int a; // lazy loading, during object creation
5+
static int b; // class member (eager loading, during class loading)
6+
7+
static {
8+
b = 1;
9+
System.out.println("this is static : b == " + b);
10+
}
11+
12+
static void show() {
13+
// a = 1000; // ! Cannot make a static reference to the non-static field
14+
System.out.println(" Show : " + b);
15+
}// static methods
16+
17+
Demo() {
18+
b++;
19+
System.out.println("This is constructor here a is : " + a + " and b is : " + b);
20+
}
21+
22+
void print() {
23+
System.out.println(" This is print : " + b);
24+
// we can also use static variables in instance variable
25+
// we can access eager variables in lazy varables and methods but its opposite
26+
// is not true;
27+
}
28+
}
29+
30+
public class StaticVsInstance {
31+
public static void main(String[] args) {
32+
Demo.show();// only static values can be used
33+
Demo ob = new Demo();
34+
ob.print();
35+
36+
Demo ob2 = new Demo();
37+
Demo ob3 = new Demo();
38+
Demo ob4 = new Demo();
39+
// a is creating in heap 4 times as it is an instance varaible, but b is created
40+
// once.
41+
42+
}
43+
}

0 commit comments

Comments
 (0)