-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticNonStaticTest.java
More file actions
54 lines (46 loc) · 1.72 KB
/
Copy pathStaticNonStaticTest.java
File metadata and controls
54 lines (46 loc) · 1.72 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
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
package random.java;
/*
* Characteristics of Static methods
A static method is called using the class (className.methodName) as opposed to to an instance reference (new instanceOfClass = class; instanceOfClass.methodName.)
Static methods can’t use non-static instance variables: a static method can’t refer to any instance variables of the class. The static method doesn’t know which instance’s variable value to use.
Static methods can’t call non-static methods: non-static methods usually use instance variable state to affect their behaviour. Static methods can’t see instance variable state, so if you try to
call a non-static method from a static method the compiler will complain regardless if the non-static method uses an instance variable or not.
*
*
Non-Static methods
A non-static method does not have the keyword static before the name of the method.
A non-static method belongs to an object of the class and you have to create an instance of the class to access it.
Non-static methods can access any static method and any static variable without creating an instance of the class.
*
*
*
*
*
*
*
* */
public class StaticNonStaticTest {
public static void main(String[] args) {
StaticNonStaticTest test = new StaticNonStaticTest("A", 23);
test.setName("bob");
System.out.println(test.getName());
}
private String name;
private int age;
public StaticNonStaticTest(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}