In order to access or test non-static variables and non-static methods, there is only one approach called 1. through the object
In order to access or test static variables and static methods we have 3 approaches
1. directly by calling their names
2. through the class name
3. through object
We have total 4 types of variables
1. static
2. non-static
3. local variables
4. object/reference type variables
3. local variables(method level):
public com.rameshsoft.automation.corejava;
public class ExamDemo{
int id=456; // within the class we can access anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
public class ExamDemo{
int id=456; // within the class we can access anywhere
private int practiceHrs=9; // within the class we can access anywhere
// we cannot access outside the class
//why because it is private
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
public class ExamDemo{
public int id=456; // within the class we can access anywhere
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere
// we cannot access outside the class
//why because it is private
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
>>>public class ExamDemo{
public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
public void hello() {
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
If the variable is public the scope is anywhere within the class or outside the class anywhere.
If it is private also within the class
Local variables:
1. when do we go to local variables
whenever we want to restrict the data to a certain portion of the area then we will go for local variables.
that portion of the area is nothing but a method here
whenever we declare the variables inside the methods such types of methods are called local variables
2. when we can say a variable is a local variable
whenever we declare the variables inside the methods such kinds of variables are called local variables.
>>>public class ExamDemo{
public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
public void hello() {
int salary=250000; // local variable
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
1.How many local variables do we have within the class?
one
int salary=250000; // declaring
>>>public class ExamDemo{
static public int id=456; // within the class we can access anywhere, static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
public void hello() {
int salary=250000; // local variable
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
whenever we declare variables inside the methods called local variables.
>>>public class ExamDemo{
public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
public void hello() {
id=91456; // not local variable, reassigning the value
int salary=250000; // local variable
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
// there is only one local variable in the above program
// id - we are using declared variable
>>>public class ExamDemo{
static public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
double id=456.456; // invalid
// variable name should be always unique
public void hello() {
id=91456; // not local variable, reassigning the value
int salary=250000; // local variable
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
>>>public class ExamDemo{
static public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
double id=456.456; // invalid
// variable name should be always unique
public void hello() {
int id=91456; // local variable, reassigning the value
int salary=250000; // local variable
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
// can have class level variable names and local variable names same.
// we have two local variables
>>>public class ExamDemo{
static public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
double id=456.456; // invalid
// variable name should be always unique
public void hello() {
int id=91456; // local variable, reassigning the value
int salary=250000; // local variable
// priority always local
System.out.println(id); // 91456
System.out.println(salary); //250000
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
>>>public class ExamDemo{
static public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
double id=456.456; // invalid
// variable name should be always unique
public void hello() {
// int id=91456; // local variable, reassigning the value
int salary=250000; // local variable
System.out.println(id);
// here we dont have id then priority goes to class and execute.
}
public void hello1() {
System.out.println(id+practiceHrs);
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
the scope of local variable is within the method only. outside the method we cannot access.
>>>public class ExamDemo{
static public int id=456; // within the class we can access anywhere, non-static variable
// out side the class also we can access
private int practiceHrs=9; // within the class we can access anywhere, non-static variable
// we cannot access outside the class
//why because it is private
double id=456.456; // invalid
// variable name should be always unique
public void hello() {
int id=91456; // local variable, reassigning the value
int salary=250000; // local variable
System.out.println(id+practiceHrs);
}
public void hello1() {
System.out.println(id);
System.out.println(salary); //no salary in class then compile time error
System.out.println(practiceHrs);
// if variable is not there inside the method then it will directly go to class level
}
// we can access this id and practicehrs within the class anywhere
public static void main(String[] args) {
ExamDemo examDemo= new ExamDemo();
System.out.println(examDemo.id);
}
}
// outside the method we cannot access local variables otherwise compile time error occur.
when do we go for local variables?
whenever we want to restrict the data to certain portion of the area then go for local variables
if you want to restrict the data to certain portion of are that is within the method only then we can go for local variable.
when we can say variable is a local variable?
whenever we declare the variables inside the methods called local variables
the scope of the local variable is within the method only, outside the method we cannot access local variables.
we can have class-level variable names and local variables names as same.
if we are trying to use within the method the priority always going to local
if we don't perform initialization of static variables and non-static variables we will get default values
initialization of local variables is compulsory otherwise compile-time error occurs
initialization of local variables is mandatory if and only if whenever we are using that variable
public void hello() {
int id; // no initialization
int salary=25000;
System.out.println(id); // here we are using local variable so we will get compile time error
}
public void hello() {
int id; // no initialization
int salary=25000;
// System.out.println(id); // here we are not using local variable so no error
}
for local variables default values concept is not applicable, mandatory we should perform initialization
whenever we declare the variable inside the method then those variables are called local variables
for local variables no access specifiers/ modifiers
for local variables, there is only one modifier/ keyword is applicable
1. final
final is allowed to stop reassignment of that variable
this is a keyword that never goes to a method of local directly goes to class
initialization should be inside the method only
object type or reference type variabel:
object type or reference type variable ia a variable that always contains object
In how many places we can create objects:
In general, we can create the object in two places
1. class level ---> non static OT/RT variable
2. method-level---> static OT/RT variable