FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
basic-programs/java/StaticDemonstration.java at master · BGatya/basic-programs · GitHub
BGatya
/
basic-programs
Public
forked from
utkarsh-shekhar/basic-programs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
basic-programs
/
java
/
StaticDemonstration.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
69 lines (52 loc) · 2.39 KB
Breadcrumbs
basic-programs
/
java
/
StaticDemonstration.java
Copy path
File metadata and controls
69 lines (52 loc) · 2.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.
Date
;
// Static imports are used to import static variables and methods as below
import
static
java
.
lang
.
Math
.
PI
;
// Static variable
import
static
java
.
lang
.
Math
.
abs
;
// Static method
/**
* This class intends to demonstrate the usage of static keyword in Java
*/
public
class
StaticDemonstration
{
private
final
String
name
;
private
static
final
String
time
=
new
Date
().
toString
();
/* Static block gets executed when the class is loaded by the JVM.
This happens at the beginning of the execution.
*/
static
{
System
.
out
.
println
(
"Class being loaded"
);
}
public
StaticDemonstration
(
String
name
) {
this
.
name
=
name
;
}
/* Main method is a static method meaning that it can be
called without requiring an instance of the containing class.
*/
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
"Value of PI: "
+
PI
);
System
.
out
.
println
(
"Value of abs(-1): "
+
abs
(-
1
));
System
.
out
.
println
(
"Time is: "
+
time
);
//static main method can use static variables in the class without an instance
greet
();
// static main method can call other static methods in the class without an instance
StaticDemonstration
staticDemonstration
=
new
StaticDemonstration
(
"Java Developer"
);
staticDemonstration
.
greetUser
();
// To call a non-static method, an instance is needed
System
.
out
.
println
(
"Name is: "
+
staticDemonstration
.
name
);
// To access non-static members, an instance is needed
// Static nested class can be initialized without requiring an instance of the enclosing class
StaticNestedClass
staticNestedClass
=
new
StaticNestedClass
();
// Non static inner class requires an instance of enclosing class to be instantiated
NonStaticInnerClass
nonStaticInnerClass
=
staticDemonstration
.
new
NonStaticInnerClass
();
}
private
static
void
greet
() {
System
.
out
.
println
(
"Hello user. Have a good day."
);
}
private
void
greetUser
() {
System
.
out
.
println
(
"Hello "
+
name
+
". Have a good day."
);
}
private
static
class
StaticNestedClass
{
private
StaticNestedClass
() {
System
.
out
.
println
(
"StaticNestedClass constructor"
);
}
}
private
class
NonStaticInnerClass
{
private
NonStaticInnerClass
() {
System
.
out
.
println
(
"NonStaticInnerClass constructor"
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL