Skip to content

Commit ecd4562

Browse files
committed
added static information
1 parent 066b166 commit ecd4562

1 file changed

Lines changed: 50 additions & 5 deletions

File tree

Java/basics.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
- [Difference between interface & abstract class](#difference-between-interface--abstract-class)
1515
- [Difference between instance variables & local variables](#difference-between-instance-variables--local-variables)
1616
- [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)
1820
- [Interning](#interning)
1921
- [Mutable & Immutable](#mutable--immutable)
2022
- [StingBuilder vs StringBuffer and immutability](#stingbuilder-vs-stringbuffer-and-immutability)
@@ -26,7 +28,6 @@
2628
- [System object](#system-object)
2729
- [Deep copy vs Shallow copy](#deep-copy-vs-shallow-copy)
2830
- [References](#references)
29-
- [Access Modifiers](#access-modifiers)
3031
- [Decompiling](#decompiling)
3132
- [Serialization](#serialization)
3233
- [I/O](#io)
@@ -133,7 +134,51 @@ Since Java is a Object Oriented language, in order to utilize the Object level f
133134
### Difference between interface & abstract class
134135
### Difference between instance variables & local variables
135136
### 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+
class StaticSample{
164+
private static final String ENDPOINT = "/test";
165+
166+
//Static block
167+
{
168+
169+
}
170+
171+
public static String fetchEndpoint(String baseUrl){
172+
return baseUrl + ENDPOINT;
173+
}
174+
175+
public void printEndpoint(){
176+
System.out.println(ENDPOINT)
177+
}
178+
}
179+
180+
```
181+
137182
### Interning
138183
String Interning is a method of storing only one copy of each distinct String Value, which must be immutable.
139184

@@ -159,7 +204,7 @@ Normally immutability in java is achieved through following steps :
159204
```
160205

161206
### 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+
163208

164209

165210

@@ -171,7 +216,7 @@ In Java String is an immutable object, i.e. everytime there is any change made t
171216
### System object
172217
### Deep copy vs Shallow copy
173218
### References
174-
### Access Modifiers
219+
175220
### Decompiling
176221
## Serialization
177222
## I/O

0 commit comments

Comments
 (0)