KEMBAR78
OOP using java (Variable in java) | PPTX
OOP - JAVA
Variables in Java
4/6/2022 1
Department of Computer
PRO – GRADE 5
Lecturer. OMEED M. M
Table of contents
 What is Variable?
 Allocating memory
 Types of variables
 Constructor
4/6/2022 2
Variable in Java
Variables used to store in our computer’s memory
Why called it variable?
Because the values inside the variables are changeable.
 Attributes - things that the object stores data in, generally
variables.
 Methods - Functions and Procedures attached to an Object and
allowing the object to perform actions
4/6/2022 3
Allocating memory
In order to store some data in our computer memory we have to
allocate some space so how we can do that? we will go through
allocating memory,
4/6/2022 4
Allocating memory
For this purpose we have to declare variables.
How we can declare variable we are going to using assignment
We can assign a value to a variable by using (Equal =) operator.
Ex.
String ANY_NAME = “ANY_NAME”
4/6/2022 5
Variable in Java
• Re-difine variable
4/6/2022 6
Variable in Java
• Println
4/6/2022 7
Types of variables
There are Three type of variables:-
1. Instance variables
2. Local variables
3. Class variables (Static variables)
4/6/2022 8
Instance variables
Instance variables Declared inside class but not inside method.
Ex.
Class test{
Int data=15; //instance variable
Float pi=3.114f; //instance variable
}
Cannot be reinitialized directly within class
Ex.
Class test{
Int data=15; //instance variable
data =20; //Error for correction we have to put it into a method
void testMethod(){
data=20 // correct;
} }
4/6/2022 9
Local variables
Local variables – Declared inside method or method parameters.
Ex.
Int areatest (int radius){
Int total = radius * radius;
Return total;
}
-These are not accessible outside method.
-They did not get default values.
4/6/2022 10
Class / Static variables
Class variables − are variables declared within a class, outside any
method, with the static keyword
-Static variable is shared between all objects because it belong to
the class. It does not belong to the object
-A static variable can be accessed without creating an instance of the
class.
4/6/2022 11
Constructor
A constructor in Java is similar to a method that is invoked when an
object of the class is created.
Unlike Java methods, a constructor has the same name as that of the
class and does not have any return type. For example,
class Test {
Test() {
// constructor body
}
}
4/6/2022 12
Types of variables
class A{
int date=50; //_________ variable
static int m=100; //_________ variable
void method(){
int n=90; //________ variable
}
} //end of class
4/6/2022 13
Any Questions ?
4/6/2022 14

OOP using java (Variable in java)

  • 1.
    OOP - JAVA Variablesin Java 4/6/2022 1 Department of Computer PRO – GRADE 5 Lecturer. OMEED M. M
  • 2.
    Table of contents What is Variable?  Allocating memory  Types of variables  Constructor 4/6/2022 2
  • 3.
    Variable in Java Variablesused to store in our computer’s memory Why called it variable? Because the values inside the variables are changeable.  Attributes - things that the object stores data in, generally variables.  Methods - Functions and Procedures attached to an Object and allowing the object to perform actions 4/6/2022 3
  • 4.
    Allocating memory In orderto store some data in our computer memory we have to allocate some space so how we can do that? we will go through allocating memory, 4/6/2022 4
  • 5.
    Allocating memory For thispurpose we have to declare variables. How we can declare variable we are going to using assignment We can assign a value to a variable by using (Equal =) operator. Ex. String ANY_NAME = “ANY_NAME” 4/6/2022 5
  • 6.
    Variable in Java •Re-difine variable 4/6/2022 6
  • 7.
    Variable in Java •Println 4/6/2022 7
  • 8.
    Types of variables Thereare Three type of variables:- 1. Instance variables 2. Local variables 3. Class variables (Static variables) 4/6/2022 8
  • 9.
    Instance variables Instance variablesDeclared inside class but not inside method. Ex. Class test{ Int data=15; //instance variable Float pi=3.114f; //instance variable } Cannot be reinitialized directly within class Ex. Class test{ Int data=15; //instance variable data =20; //Error for correction we have to put it into a method void testMethod(){ data=20 // correct; } } 4/6/2022 9
  • 10.
    Local variables Local variables– Declared inside method or method parameters. Ex. Int areatest (int radius){ Int total = radius * radius; Return total; } -These are not accessible outside method. -They did not get default values. 4/6/2022 10
  • 11.
    Class / Staticvariables Class variables − are variables declared within a class, outside any method, with the static keyword -Static variable is shared between all objects because it belong to the class. It does not belong to the object -A static variable can be accessed without creating an instance of the class. 4/6/2022 11
  • 12.
    Constructor A constructor inJava is similar to a method that is invoked when an object of the class is created. Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example, class Test { Test() { // constructor body } } 4/6/2022 12
  • 13.
    Types of variables classA{ int date=50; //_________ variable static int m=100; //_________ variable void method(){ int n=90; //________ variable } } //end of class 4/6/2022 13
  • 14.