-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticInNonStatic.java
More file actions
24 lines (23 loc) · 1.21 KB
/
Copy pathStaticInNonStatic.java
File metadata and controls
24 lines (23 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.oops2.StaticExample;
public class StaticInNonStatic {
public static void main(String[] args) {
StaticInNonStatic obj = new StaticInNonStatic();
obj.nonstaticfun();
}
void greeting () {
System.out.println("Hello, World");
}
static void staticfun () {
//greeting(); // We cant make call nonstatic method from static method cause nonstatic one required obj to access it but static accessible wt any obj
// So we have to create obj for access of nonstatic method
System.out.println("Hey i am static fun, u don't req any obj to access me");
StaticInNonStatic obj = new StaticInNonStatic();
obj.greeting();
}
// Calling a static and nonstatic fn from a nonstatic fn
void nonstaticfun () {
System.out.println("Hey i am non static fn, u req an obj to access me");
staticfun(); // We can call static fn from a nonstatic fn because the nonstatic fn will be accessible when an obj will be created and then static one will run on by own
greeting(); // It depends on the object of nonstaticfun when it arrange obj for itself then the same obj will be used by greeting fn for comes in work
}
}