Repository navigation
Expand file tree
/
Copy pathTest.java
More file actions
73 lines (57 loc) · 1.72 KB
/
Copy pathTest.java
File metadata and controls
73 lines (57 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Test{
/*
1. Properties/Variables/Fields
2. Methods
3. Constrcutors
4. Blocks
1. Properties/Variables/Fields
---------------------------------
Two types of Properties
-> instance Properties
-> static properties
instance Properties
----------------------
-> Values of instance properties are
available only to a specified object
static properties
------------------
-> Values of static properties are
available to all the obejcts.
-> static data can be accessed directly with out help of object
-> If The static data is defined in another class, then
data can be accessed with class name
2. Methods
-----------
Two Types
-> instance methods
-> non static method allows instance and static data
-> instance methods must be called with a object, if you
are calling from a static method
-> static methods
-> static methods allows static data directly
-> static methods never allow instance data directly
Q1: Why I need object?
-> To access non static data from a static method in the same class
-> To access non static data from a non static method in the
different class
*/
int x=1000; // instance variable
static int y=2000; // static variable
void f1(){ // non static -> instance
x=877;
y=7777;
}
static void f2(){ // static method
Test obj=new Test();
y=77772;
obj.x=7777;
obj.f1(); // valid
}
public static void main(String[] abc){
//System.out.println("--- hello world");
Test t1= new Test();
Test t2= new Test();
System.out.println(y);
System.out.println(t1.x);
}
}