You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Java/basics.md
+50-5Lines changed: 50 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,9 @@
14
14
-[Difference between interface & abstract class](#difference-between-interface--abstract-class)
15
15
-[Difference between instance variables & local variables](#difference-between-instance-variables--local-variables)
16
16
-[Object creation](#object-creation)
17
-
-[Static vs instance methods](#static-vs-instance-methods)
17
+
-[Access Modifiers](#access-modifiers)
18
+
-[Other modifiers](#other-modifiers)
19
+
-[Static](#static)
18
20
-[Interning](#interning)
19
21
-[Mutable & Immutable](#mutable--immutable)
20
22
-[StingBuilder vs StringBuffer and immutability](#stingbuilder-vs-stringbuffer-and-immutability)
@@ -26,7 +28,6 @@
26
28
-[System object](#system-object)
27
29
-[Deep copy vs Shallow copy](#deep-copy-vs-shallow-copy)
28
30
-[References](#references)
29
-
-[Access Modifiers](#access-modifiers)
30
31
-[Decompiling](#decompiling)
31
32
-[Serialization](#serialization)
32
33
-[I/O](#io)
@@ -133,7 +134,51 @@ Since Java is a Object Oriented language, in order to utilize the Object level f
133
134
### Difference between interface & abstract class
134
135
### Difference between instance variables & local variables
135
136
### Object creation
136
-
### Static vs instance methods
137
+
### Access Modifiers
138
+
Java uses different access modfiers to set the access levels on variables, methods, constructors and classes.
139
+
140
+
* Visible to the package, the default. No modifiers are needed.
141
+
* Visible to the class only - `private`
142
+
* Visible to the world - `public`
143
+
* Visible to the package and all subclasses - `protected`
144
+
145
+
### Other modifiers
146
+
Apart from the access modifiers, the below keywords aka Non-Access modifiers can be used to achieve different functionalities.
147
+
* Creating class methods and variables - `static`
148
+
* Finalizing the implementations of classes, methods, and variables - `final`
149
+
* Creating abstract classes and methods - `abstract`
150
+
* For threads - `synchronized` & `volatile`
151
+
152
+
### Static
153
+
Static variables are also known as class variables. These variables exist independently of creation of instances. They can be accessed directly via classes. Regardless of how many instances of a class is created, there will always be a single copy static variable exist. They can be accessed using `ClassName.variable`.
154
+
155
+
Similar to variables, methods also can be marked as static to make them class methods. They can be called using `ClassName.method()`.
156
+
157
+
Since static methods are tied to class, it can not use any instance variable or methods (non static). But non-static methods can access or call static variables/methods.
158
+
159
+
Static block is used for initializing the static variables.This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program
160
+
161
+
```java
162
+
163
+
classStaticSample{
164
+
privatestaticfinalStringENDPOINT="/test";
165
+
166
+
//Static block
167
+
{
168
+
169
+
}
170
+
171
+
publicstaticStringfetchEndpoint(StringbaseUrl){
172
+
return baseUrl +ENDPOINT;
173
+
}
174
+
175
+
publicvoidprintEndpoint(){
176
+
System.out.println(ENDPOINT)
177
+
}
178
+
}
179
+
180
+
```
181
+
137
182
### Interning
138
183
String Interning is a method of storing only one copy of each distinct String Value, which must be immutable.
139
184
@@ -159,7 +204,7 @@ Normally immutability in java is achieved through following steps :
159
204
```
160
205
161
206
### StingBuilder vs StringBuffer and immutability
162
-
In Java String is an immutable object, i.e. everytime there is any change made to the String object, a new object will be created (or referred).
207
+
163
208
164
209
165
210
@@ -171,7 +216,7 @@ In Java String is an immutable object, i.e. everytime there is any change made t
0 commit comments