-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifier.java
More file actions
32 lines (22 loc) · 861 Bytes
/
modifier.java
File metadata and controls
32 lines (22 loc) · 861 Bytes
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
public class modifier {
final int x = 10;
final double PI = 3.14;
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[ ] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would output an error
modifier myObj = new modifier(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method
//myObj.x = 50; // will generate an error: cannot assign a value to a final variable
//myObj.PI = 25; // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}