Tuesday, August 31, 2021

# 15. Local Variables

 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














Saturday, August 28, 2021

# 14. java source file structure

 If we want to execute the program in java compulsory jvm always starts the execution from the main method.

whenever the programmer asks the jvm to run the program jvm always looks for main method.

main method is the starting point for the program.

If we dont define main method immediately we will get compile time error.

If there is no main method we cannot execute the program


JAVA Source file structure:

Source file is nothing but where we are going to write our programs

1. In a single source file we can have multiple classes/ class files

>>> class A1{

------

}

class A2{

-------

}

classA3{

-------------

}

2. If no class is declare as public then we can save that file with any name

A1.java

A2.java

A3.java

A4.java

Hello.java

Anything.java

3. It is a good programming practice that declare one class as public, the class which we declared as publice the public class and filename must be same otherwise we will get compile time error.

>>> class A1{

------

}

public class A2{

-------

}

classA3{

-------------

}

Ex: A2.java ===>valid

A1.java ===>invalid

A3.java===> invalid

Hello.java ===>invalid

4. If we are trying to define more than one class as public immediately we will get compile time error

  In a single source file we should not take more than one class as a public.

>>> class A1{

------

}

public class A2{

-------

}

public classA3{

-------------

}

// we will get compile time error

5. The class which we declare as a public in that public class only we must and should take main method.

>>> class A1{

------

}

class A2{

-------

}

public classA3{

public static void main(String[] args)

{

-------------

}// end of main

}// end of class


Benifits of ide/eclipse:

We need to focus on 

1. perspective(on what perspective u want to write the programs)

2.eclipse editor (where we are going to write our programs

3. console (where we can see the output)

4. project explorer/package explorer (where we are going to manage our projects)

5. git

6. debug


Eclipse:

right side click on + symbol

select java(default)

firse we need to create a project

goto file--->new--->project--->java project--->next---> projectname and version--->next--->finish

project got created

1.jre system library

2. src (source folder where we can write our programs)

second way to create project :

goto window---> show view--->click on console---> window--->show view--->package/project explorer

Whenever we create a project two files are going to be created automatically

Assume that the Flipkart application

we have three modules

1. men-------> 25 classes

2. women----> 20 classes

3. kids-------->45 classes

under the men module, we need to write  25 classes like that

src---> class names like men1,men2,women1,women2,kids1,kids2 etc

we have taken 90 classes

if we want to execute only kids classes then we need to look for those classes in 90 classes

if we want to look only men classes then we need to look for men classes out of 90 classes

its become complex

for example under kids module if we have different types of chacolates like 100 5star,150 kitkat,200 dairymilk, etc

if all are combined together in one box.

instead of that if we put them separately in boxes its very easy to get them.

in the same way dont write all your programs under src folder.

under src folder create packages

package is a collection of classes + interfaces+enums.

Standard syntax for package:

package <packagename>

package is a fixed keyword inorder to convey it as a package

pacakagename: can be anything but it should be very meaningful.

package men;

inside this men all the program related to men

package women;

package kids;

But this above are not valid in real time we have some standard sysntax:

domain name in reverse. modulename. submodulename

www.flipkart.com

package com.flipkart.men;

src---> new--->package

give some name like 

package com.flipkart.men;

package com.flipkart.women;

package com.flipkart.kids;


now to write all men program select this package--->new-->class---> give class name

how many classes we can write all classes

men1,men2,men3,women1,women2,women3,kids1,kids2,kids3,....... etc...

to execute kids class simply look for kids package

To run: rightclick--->run as java application

package com.flipkart.kids;

public class kids3{

 public void hello() {

 System.out.println("hello");

}

public void job(){

System.out.println("job");

}

public static void main(String [] args) {

  System.out.println("hello");

}

}

















 








Friday, August 27, 2021

# 13. main method

 In order to print some information on the console, in java, we have a method called 

System.out.println() method.

Standard syntax:

System.out.println();

>>>class Demo{

int a1=456, a2= 789;

static String name="RAMESHSOFT"

public int addition()

{

int result =a1+a2;

return result;

}

}

Ex:

int a1=456, a2= 789;

static String name="RAMESHSOFT"

Syntax:

1.System.out.println(String message);

System.out.println("Please do practice and it is mandatory");

Whatever we write in double-quotes as it is it will print on the console.

2. To print variable values on the console

System.out.println(variablename);

To print more than one variable

System.out.println(varname1+varname2+varname3+.......);

>>>System.out.println(a1);

output: 456

>>>system.out.println("a1");

Output: a1

>>> System.out.println(a1+a2);

here addition is going to be happen

output: 1245

here + is acting like a addition

System.out.println(a1+name);

output: 456 RAMESHSOFT

concatenation happened.

here + is acting like a concatenation

In java the only one overloading operator is + , sometimes it acts like addition and sometimes it acts like concatenation

>>> System.out.println(a1+name+a2);

Output: 456 RAMESHSOFT 789

>>> System.out.println(a1=a2+name);

Output: 1245 RAMESHSOFT

3. To print some message along with variable value

syntax:

System.out.println(String msg+variablename);

or

System.out.println(variablename+String msg);

>>> System.out.println("The value of a is:"+a1);

>>>System.out.println(a1+" The value of a is");

Output: 

The value of a is: 456

456 The value of a is

4. To print morethan one variable with some message

System.out.println(String msg1+variablename1+ String msg2+variablename2);

or

System.out.println(variablename1+String msg1+variablename2+String msg2);

Example:

>>> public class Addition{

public int numberOne=45;

public int numbertwo=47;

public int addition()

{

int result=numberOne+numberTwo;

return result;

System.out.println("result is: "+result);    

}

}

Whenever we compile this program we will get a compile-time error because after the return statement we should not write any statements.

A method should not contain more than one return statement.

per method we should write only one statement otherwise we will get a compile-time error.

return statement should be the last statement inside a method.

>>> public class Addition{

public int numberOne=45;

public int numbertwo=47;

public int addition()

{

int result=numberOne+numberTwo;

System.out.println("result is: "+result);  

return result;  

}

}

It will compile without any errors.

In the above program inside the method, we implemented the business logic of addition between two numbers.

Whenever we call this method it is going to be performed addition between two numbers and return result.

1. We implemented one business logic then we need to testing that it is working correctly or not.

2 in order to write a program in java we need to follow 3 standard steps

2.1 write a program

 save with .java extension

2.2. validate/compile

2.3. execute

In order to execute the java program standard syntax is

<javac>    <class name>  .java

To execute standard syntax is

,java>    <classname>

>>> javac Addition_Demo.java

>>> java Addition_Demo

The moment we click on enter here JVM is going to start. once JVM is started in this desktop JVM check this .class file is there or not.

JVM loads the .class file, whenever JVM is loading the .class file automatically all the static variables and static methods are going to be created.

JVM is going to look execution 

Whenever u ask the JVM to run the program JVM is going to look for the starting point to execute the program

Ex: to start a car we need a key point

All the static blogs are going to be executed automatically.

1. check for the class file

2. JVM looks for .class file

3. loads the .class file

4. JVM looks for execution point

What is the execution point?

Jvm is always going to starts the execution from the main() method.

the main method is the starting point for the execution

If the main method is not there we will get an error.

What is the main method in java?

In java, the main method is there for two reasons

1. whenever u ask the JVM to run the program, JVM always starts execution from the main method.

why?

JVM is internally configured in such a way that whenever the programmer is asked to run the program always starts the execution from the main method.

if the main method is not there JVM doesn't know where to start the execution that's why it's throwing a compile-time error.

* In order to execute a program, we need a main method

2. Inside the main method what we will do?

We will do testing

How to define the main method:

Standard syntax:

1. public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}


2. static public void main(String[] args)

{

// accessing or testing the implemented services or methods

}


3. public static void main(String... args)

{

// accessing or testing the implemented services or methods

}

In place of the args whatever we want we can write

4. public static void main(String[] RAMESHSOFT)

{

// accessing or testing the implemented services or methods

}

5. public static void main(String... RAMESHSOFT)

{

// accessing or testing the implemented services or methods

}

/* In place of [] we can write ... three dots

6. strictfp public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

All the above main methods are valid but commonly we use first method

Main method is bydefault static method.

Where we need to define the main method

1. After starting the class immediately take main method

<as>    class   <classname>  {

public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

;;;;;;;;;;;;;;;;;;;///defined or implemented part

;;;;;;;;;;;;;;;;;

}

2. Before end of a class take main method

<as>    class   <classname>  {

;;;;;;;;;;;;;;;;;;;///defined or implemented part

;;;;;;;;;;;;;;;;;

public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

}

3. In between the class take main method

<as>    class   <classname>  {

;;;;;;;;;;;;;;;;;;;///defined or implemented part

;;;;;;;;;;;;;;;;;

public static void main(String[] args)

{

// accessing or testing the implemented services or methods

}

;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;; //implemented or defined part

}

4. Take main method in seperate class

<as>    class   <classname>  {

;;;;;;;;;;;;;;;;;;;///defined or implemented part/services part

;;;;;;;;;;;;;;;;;

}

<as>    class   <classname>  {

}

<as>    class   <classname>  {

public static void main(String [] RAMESHSOFT)

{

// accessing or testing the implemented methods / services

// execution part

}

}


>>> public class Addition_Demo{

public int numberOne=45;

public int numbertwo=47;

public int addition()

{

int result=numberOne+numberTwo;

System.out.println("result is: "+result);  

return result;  

}

public static void main(String[] RAMESHSOFT)

{

System.out.println("JVM started execution")

}//end of main method

}//end of class

there is nothing to execute

.

When this above method is going to be executed?

r u calling that method?

no

so it won't respond

We implemented one business service we need to call it

>>>javac Addition_Demo.java

>>>java Addition_Demo


public static void main(String[] RAMESHSOFT)

public void main(String[] RAMESHSOFT)

if we remove static we will get a compile-time error.

public static void main(String[] ...) ===> valid



































Thursday, August 26, 2021

# 12. non static method

 non-static method:

whenever the business implementations are not constant then we go for the non-static method.

standard syntax:

1. <access specifier>    void      methodname()

{

;;;;;;;;;;;;;;;;;;;;;;;

}

2. <access specifier> return type methodname()

{

;;;;;;;;;;;;;;;;;;;;;;;

return <statement>;

}

BR: WAP for the addition of 2 numbers

45+45=90

/* using void type

public class Addition_Program{

int num1=45;

int num2=45;

public void addition_Program()

{

int result=num+num2;

}

}

/* Using return type:

public class Addition_Program{

int num1=45;

int num2=45;

public int addition_Program()

{

int result=num+num2;

return result;

}

}

float, long, double are valid because they are higher than int

byte short is invalid.


Monday, August 23, 2021

# 11. static method

 Method:

Depends upon the data behavior we have two methods.

1. Static :

------------

When do we go for static methods?

case 1: Whenever the business implementations are not going to change or constant then we will go for constant

case 2: Whenever we want to create read-only services

case 3: Without creating an object we want to access services

2. Non-static:

-----------------

when do we go for non-static methods?

Whenever the implementations are not constant then we will go for non-static.


How to define a static method:

Syntax:

<access specifier>    static    void    methodname(--------)

{

------------------

}

  static   <access specifier>    void    methodname(--------)

{

------------------

}

<access specifier>    static    <return type>    methodname(--------)

{

------------------

return <stmt>;

}

  static   <access specifier>    <return type>    methodname(--------)

{

------------------

return <stmt>;

}


Access specifier is not mandatory, it's optional

Specifying access of a class

If we want to put some restrictions on our business method

Static: 

static is a keyword to convey it as a static method

Void:

it returns nothing

whenever we are keeping the method void, the business implementations are going to be executed but in the end, it won't return anything

Whenever you are keeping return type after executing the business implementations it's going to return some values.


Method name rules:

every method must and should have some name

it should start with a small letter

it should not start with numbers or special characters

method name should be clear and very meaningful

if a method name has multiple words every first word should start with a capital letter except the first word

Ex: addition_Program

As a part of the return type what should allow?

void is allowed

primitives like int, short, byte, long, float, double, boolean, char, 

class type as string allowed

interface type also allowed


<access specifier>    static    <return type>    methodname(--------)

{

------------------

return <stmt>;          /* is mandatory

}

return <stmt>; =======>

whenever our method is having a return type then the return statement is mandatory

we should not write anything after the return statement.

the return type is always based on return statement

return type should be same types as return statement of integer or higher than integer

if we are trying to return lower than integer then we will get compile time error

return statement should be the last statement inside the method otherwise we will get compile time error.

After the return statement, we should not take any statement otherwise we will get a compile-time error.

if we want to return some values then go for return type

if return statement is returning some type then the return type should be same or higher than that type.

but not lower otherwise we will get compile time error.

For the business method, we should take only one return statement we should not take more than one otherwise we will get a compile-time error.

Real-time approach:

ATM:

BR: Cash deposit:

----------------------

savings balance=25000

insert the atm card

valid card or not

if it is invalid===> invalid card

card is valid

enter the account number

insert the amount

it goint to save the amount

amount is deposited

25000+10000=35000

savings balance=35000

After the cash is deposited we want to return the savings balance

case1: if you want to return the savings balance at the end of the business implementations then go for <return type>.

case2: if you don't want to return the savings balance then go for <void type>.

For example take a calculator and perform 25+20 then it will return 45 value.


loginTestDemo() ===> valid

for more readability differentiate with _

login_Test_Demo() ===> more readable


class Demo{

int num1=25;

int num2=65;

or

int num1=25, num2=65;

public static void additionProgram()     /* it wont return anything because void

{

int result=num1+num2;

}

}

if we want to see the result or return something then

class Demo{

int num1=25, num2=65;

public static int additionProgram()     /* it wont return anything because of void

{

int result=num1+num2;

return result;

}

}

Lower to higher:

byte ===> short ===> int ===> long ===> float ===> double 

                                    char

int can hold char long float double 

int cannot hold byte short





















Saturday, August 21, 2021

# 10. Methods

 BR: WAP for the addition of two numbers

public class Addition_Program {

int number_One=45;

int number_Two=47;

number_One + number_Two =====> whenever we add these two numbers we will get 97

Here 97 is also data that should be placed inside a container called a variable.

We need to declare a new variable to place that value

97 is of type integer

}

----------------------------------------------------------------------

public class Addition_Program {

/* These two lines are like ingrediants

int number_One=45;

int number_Two=47;

/* business logic/ implementation

int result = number_One + number_Two

}


Now compile and execute

>>> javac Addition_Program.java

But this business logic/implementation of a business requirement should not be a normal statement that should be under inside one more container called Methods.

Methods:

If we want to write business logic/ business implementation it should be there inside a container called method.

A class contains variables and methods

There are two types of methods:

1. Static methods

2. Non-static methods




Friday, August 20, 2021

# 9. non static variables

Q) Where do we need to declare static variables?

 After starting the class immediately declare the static variable.

RAMESHSOFT ====> is a data

public static string Name = "RAMESHSOFT";

If we call that institute Name we will get RAMESHSOFT

In java initialization of the static variables is not mandatory, it is optional.

If we don't declare initialization of static variables then JVM is going to assign the default values

static int number_Two; ===> Valid

public static int number_Two; ===> Valid

Default values:

byte    short    int    long ===> 0

float    double ==========>0.0

String ================> null

boolean ==============> false

static int number_Two;   ===== > 0

static String ifYouDontDoPractice; =====> null

Whenever we declare the variable depends upon the data type for every variable internally some memory is going to be allocated.

Everything in the world occupies some space


Q) How to define non-static variables

Standard syntax:

If there is no static keyword then it will be non-static syntax

<access specifier>      data-type       variable-name    =     value/data ;

public int number_Two = 34;

<access specifier>       data-type        variable-name ;   

variable-name       =     value/data ;

public int number_Two ;

number_Two = 34 ;

Access specifier is not mandatory it is an optional


public int id=456; ========> non-static variable

static string target="java"; ==> static variable

double d=456.45; ========> non-static variable

float f1=456.456f; ========> non static variable

static int practiceHrs=9; ===> static variable

Q) Where do we need to declare non-static variables?

After starting the class immediately we need to declare.


If we dont specify initialization of variable then JVM will automatically assign some default value.

Q) Default values applicable for non-static variables?

Yes

byte    short    int    long ===> 0

float    double ==========>0.0

String ================> null

boolean ==============> false.


// Write addition program 45+47=92

// Assume 45 and 47 going to be changes means non-static

public class Addition_Program {

public int number_One=45;

public int number_Two=47;

}


 Example:


public class Addition_Demo{        // access specifier, class keyword, class name, start of the class


// declare static variables

// declaration and initialization in single line

static public int num1=10;        //data is constant,access specifier,data type,variable name,data

static int num2=20;        // access specifier is optional, data is constant, data type, variable name, data

public static int num3=30;        // access specifier, data is constant, data type, variable name, data


// declaration and initialization in two lines

static int num4;        // access specifier is optional, data is constant,data type, variable name

num4=40;        // variable name, data


// declare non static variables

// declaration and initialization in one line

public int num5=50;        // data changes, access specifier, data type, variable name,data

int num6=60;        // data changes,access specifier is optional, data type, variable name, data

int num7;        // data changes, access specifier is optional, data type ,variable name

num7=70;        // variable name,data


// if we don't initialize then, the default values are going to be assigned by JVM

// No initialization 

// Only declaration

// for static

static public int num8;        //default value for int =0

static float num9;        //default value for float=0.0

static String name;        //default value for String=null


// for non static

public int num9;        //default value for int=0

float num10;        // default value for float=0.0

String name;        // default value for String=null


}        //end of the class



# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...