Day-16:variable explanation
Default Values: Global Variables - Fields static - static keyword [Class specific] non-static - no keyword [Object specific] Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another. Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change. Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class In cases, where we do not initialize any global variable [Field], Default values will be given to them - by Java. byte, short, int, long --> 0 float, double --> 0.0 char --> '' boolean --> false String --> null public class Tourist { //Shopping, Mall, static int amount = 100000; public static void main(String[] args) { System.out.println(Tourist.amount); //static System.out.println(amount);//static Tourist tourist1 = new Tourist(); tourist1.having_dinner(); } public void having_dinner() { int amount = 2000; System.out.println("Dinner"); } } class Players { //global variable -> Object specific data String name; //non-static /instance / Object specific int score; boolean played; public static void main(String[] args) { Players player1 = new Players(); Players player2 = new Players(); player1.name = "dhoni"; player1.score = 30; System.out.println(player1.name); System.out.println(player1.score); System.out.println(player2.name); System.out.println(player2.score); System.out.println(player1.played); player1.play(); player2.play(); } public void play() { int balls; System.out.println(balls); } } Reference link: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Default Values:
Global Variables - Fields
- static - static keyword [Class specific]
- non-static - no keyword [Object specific]
Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class
In cases, where we do not initialize any global variable [Field], Default values will be given to them - by Java.
byte, short, int, long --> 0
float, double --> 0.0
char --> ''
boolean --> false
String --> null
public class Tourist
{
//Shopping, Mall,
static int amount = 100000;
public static void main(String[] args)
{
System.out.println(Tourist.amount); //static
System.out.println(amount);//static
Tourist tourist1 = new Tourist();
tourist1.having_dinner();
}
public void having_dinner()
{
int amount = 2000;
System.out.println("Dinner");
}
}
class Players
{
//global variable -> Object specific data
String name; //non-static /instance / Object specific
int score;
boolean played;
public static void main(String[] args)
{
Players player1 = new Players();
Players player2 = new Players();
player1.name = "dhoni";
player1.score = 30;
System.out.println(player1.name);
System.out.println(player1.score);
System.out.println(player2.name);
System.out.println(player2.score);
System.out.println(player1.played);
player1.play();
player2.play();
}
public void play()
{
int balls;
System.out.println(balls);
}
}
Reference link:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html