-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatic_FinalKeywords.java
326 lines (289 loc) · 7.34 KB
/
Static_FinalKeywords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package Examples;
// Modify static variable in main method and other methods of the class
/*
public class Static_FinalKeywords {
static int a = 10;
static int b;
public static void printStaticValue() {
a = a / 2;
b = a;
System.out.println("From printStaticValue: Value of a is " + a + " & b is " + b);
}
public static void main(String[] args) {
printStaticValue();
b = a * 5;
System.out.println("From main: Value of a is " + a + " & b is " + b);
}
}
*/
// Static variable as a counter
/*
class Counter {
static int counter = 0;
Counter(){
counter++;
System.out.println("Value of counter is: " + counter);
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
System.out.println("Value of Static Variable (Counter): ");
Counter Obj1 = new Counter();
Counter Obj2 = new Counter();
Counter Obj3 = new Counter();
}
}
*/
// Static Method
/*
public class Static_FinalKeywords {
public static void Static_Method() {
System.out.println("Called Without making any object");
}
public static void main(String[] args) {
Static_Method(); // Called without making any object
}
}
*/
// Static Method Overloading
/*
public class Static_FinalKeywords {
public static void Static_Method() {
System.out.println("Method without any parameters");
}
public static void Static_Method(String args) { // Method Overloading
System.out.println("This is a Method using parameter -> " + args);
}
public static void main(String[] args) {
Static_Method("This is arrgument");
Static_Method();
}
}
*/
// Static Method Overriding
/*
class parentClass {
public static void Static_Method() {
System.out.println("Parent Class -> Static Method");
}
}
class childClass extends parentClass{
public static void Static_Method() {
System.out.println("Child Class -> Static Method");
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
parentClass parent = new parentClass();
childClass child = new childClass();
parent.Static_Method();
child.Static_Method();
}
}
*/
// Static Block
/*
public class Static_FinalKeywords {
static int sum;
static int a = 3;
static int b = 4;
// Static
static{
sum = a + b;
System.out.println("In Static Block: ");
System.out.println("Values of a: " + a + "; b: " + b + "; Sum: " + sum);
a = b * 2;
sum = a + b;
}
public static void main(String[] args) {
System.out.println("In Main method: ");
System.out.println("Values of a: " + a + "; b: " + b + "; Sum: " + sum);
}
}
*/
// Multiple Static Blocks
/*
public class Static_FinalKeywords {
static int num;
static int sum;
static{
num = 8;
sum = 1282;
}
// In sequential static blocks, values of 1st block overwrites values of 2nd block.
static{
num = 10;
sum = 2002;
}
public static void main(String[] args) {
System.out.println("Value of num : " + num + ", sum: " + sum);
}
}
*/
// Calling Static & Non-Static Methods
/*
public class Static_FinalKeywords {
static int num = 2002;
static String name = "Cee";
public static void static_display() {
System.out.println("Number : " + num + "; Name: " + name);
}
void nonStatic_display(){
System.out.println("Number: " + num + "; Name: " + name);
}
public static void main(String[] args) {
Static_FinalKeywords Obj = new Static_FinalKeywords();
Obj.nonStatic_display();
static_display();
}
}
*/
// Static Binding
/*
class human {
public static void Generation() {
System.out.println("An honurable guy");
}
}
class boy extends human{
public static void Generation() {
System.out.println("No self respect");
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
human Obj1 = new boy();
human Obj2 = new human();
Obj1.Generation();
Obj2.Generation();
}
}
*/
// Dynamic Binding
/*
class human {
public void Generation() {
System.out.println("An honurable guy");
}
}
class boy extends human{
public void Generation() {
System.out.println("No self respect");
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
human Obj1 = new boy();
human Obj2 = new human();
Obj1.Generation();
Obj2.Generation();
}
}
*/
// Memory Efficient Static Variable
/*
class Example {
int rollNum;
String collegeName = "XYZ";
String studentName;
Example(int rollNum, String studentName){
this.rollNum = rollNum;
this.studentName = studentName;
}
public void method() {
System.out.println("Name: " + studentName + "; Roll No: " + rollNum + "; College: " + collegeName);
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
Example Obj1 = new Example(24, "Cee");
Example Obj2 = new Example(8, "Cench");
Obj1.method();
Obj2.method();
}
}
*/
// Counter without static variable
/*
class Counter {
int counter = 0;
public void increment() {
counter++;
System.out.println(counter);
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
Counter Obj1 = new Counter();
Counter Obj2 = new Counter();
Counter Obj3 = new Counter();
// Doesn't increment value, bcz counter variable is not static.
Obj1.increment();
Obj2.increment();
Obj3.increment();
}
}
*/
// Counter with static varibale
/*
class Counter {
static int counter = 0;
public void increment() {
counter++;
System.out.println(counter);
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
Counter Obj1 = new Counter();
Counter Obj2 = new Counter();
Counter Obj3 = new Counter();
// Increments value, bcz counter variable is static.
Obj1.increment();
Obj2.increment();
Obj3.increment();
}
}
*/
// Static Method performing Calculation
/*
public class Static_FinalKeywords {
static int num;
public static int Cube(int num) {
return num*num*num;
}
public static void main(String[] args) {
System.out.println(Cube(9));
}
}
*/
// Static Block invoked
/*
public class Static_FinalKeywords {
static{
System.out.println("This block is always called...");
}
public static void main(String[] args) {
System.out.println("Main method");
}
}
*/
// Static & Non-Static Methods
class staticMethods {
public int multiply(int num1, int num2) {
return num1 * num2;
}
// Static Method
public static int addition(int num1, int num2) {
return num1 + num2;
}
}
public class Static_FinalKeywords {
public static void main(String[] args) {
staticMethods Obj1 = new staticMethods();
int prod = Obj1.multiply(3, 4);
int sum = staticMethods.addition(2, 2);
System.out.println("(Non-Static) Product is: " + prod);
System.out.println("(Static) Sum is: " + sum);
}
}