Skip to content

Commit 30c42b0

Browse files
committed
README update regarding Variable Scope
1 parent 6607a31 commit 30c42b0

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,75 @@ Categories of Operators :
273273

274274
Note : The Code examples are given in the code file named as "operatr.java" above.
275275

276+
## Variables in JAVA
277+
278+
In Java, Variables are the data containers that save the data values during Java program execution. Every Variable in Java is assigned a data type that designates the type and quantity of value it can hold. A variable is a memory location name for the data.
279+
280+
### Categories of Variables :
281+
282+
Below we have different categories of variables with example explanation :-
283+
284+
- **Local** : A variable defined within a block or method or constructor is called a local variable.These variables are created when the block is entered, or the function is called and destroyed after exiting from the block or when the call returns from the function.
285+
286+
Syntax Code :
287+
288+
```
289+
class Sample{
290+
public statc void main(String[] a){
291+
292+
// defining a local variable here
293+
int number = 12;
294+
295+
// Local to main method only
296+
System.out.println("Local variable : " + number);
297+
}
298+
}
299+
```
300+
301+
- **Instance** : Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block.As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
302+
303+
Syntax Code :
304+
305+
```
306+
class Sample{
307+
308+
// Creating an instance variable here
309+
public int num1 = 12;
310+
public int num2 = 22;
311+
312+
public static void main(String[] a){
313+
314+
// Accessing instance variable
315+
Sample one = new Sample();
316+
317+
System.out.println("First Instance variable : " + one.num1);
318+
System.out.println("Second Instance variable : " + one.num1);
319+
320+
}
321+
}
322+
```
323+
324+
- **Static** : These variables are declared similarly to instance variables. The difference is that static variables are declared using the static keyword within a class outside of any method, constructor, or block.Static variables are created at the start of program execution and destroyed automatically when execution ends.
325+
326+
Syntax Code :
327+
328+
```
329+
class Sample{
330+
331+
// Defining a static variable
332+
public static int number = 34;
333+
334+
public static void main(String[] a)[
335+
336+
// Accessing the static variable here
337+
System.out.println("Static variable declared inside the class : " + number);
338+
339+
]
340+
}
341+
```
342+
343+
Note : Further Explanation is been given in the code file above with name "var1.java".
344+
276345
### Social Media Links :
277346

278347
- [Instagram Page](https://www.instagram.com/coding.needs/)

0 commit comments

Comments
 (0)