SlideShare a Scribd company logo
1 of 17
Computer
Programming 2
Lesson 6– JAVAVARIABLESAND
MODIFIERS
Prepared by: Analyn G. Regaton
JAVA
VARIABLE
 A variable provides us with named storage that our programs can
manipulate. Each variable in Java has a specific type, which
determines the size and layout of the variable's memory; the range
of values that can be stored within that memory; and the set of
operations that can be applied to the variable.
 You must declare all variables before they can be used. Following is
the basic form of a variable declaration −
Here data type is one of Java's datatypes and variable is the name
of the variable. To declare more than one variable of the specified
type, you can use a comma-separated list.
Data type variable [ = value], variable [ = value] ,,,];
Following are
valid examples
of variable
declaration
and
initialization in
Java
This lesson will explain various variable
types available in Java Language. There are
three kinds of variables in Java −
 Local variables
 Instance variables
 Class/Static variables
LOCAL
VARIABLES
 Local variables
 are declared in methods, constructors, or blocks.
 are created when the method, constructor or block is
entered and the variable will be destroyed once it exits
the method, constructor, or block.
 Access modifiers cannot be used for local variables.
 Local variables
 are visible only within the declared method,
constructor, or block.
 are implemented at stack level internally.
 There is no default value for local variables, so local
variables should be declared and an initial value
should be assigned before the first use.
EXAMPLE
 Here, age is a local variable. This is defined inside pupAge() method
and its scope is limited to only this method.
This will produce the following result −
Output
Puppy age is : 7
ANOTHER
EXAMPLE
Following example uses age without initializing it, so it
would give an error at the time of compilation.
This will produce the following error while compiling it −
Output
INSTANCE
VARIABLE
Instance variables
 are declared in a class, but outside a method,
constructor or any block.
 When a space is allocated for an object in the heap,
a slot for each instance variable value is created.
 are created when an object is created with the use
of the keyword 'new' and destroyed when the
object is destroyed.
 hold values that must be referenced by more than
one method, constructor or block, or essential parts
of an object's state that must be present throughout
the class
 can be declared in class level before or after use.
INSTANCE
VARIABLE
 Access modifiers can be given for instance variables.
 The instance variables are visible for all methods, constructors
and block in the class. Normally, it is recommended to make
these variables private (access level). However, visibility for
subclasses can be given for these variables with the use of access
modifiers.
 Instance variables have default values. For numbers, the default
value is 0, for Booleans it is false, and for object references it is
null. Values can be assigned during the declaration or within the
constructor.
 Instance variables can be accessed directly by calling the variable
name inside the class. However, within static methods (when
instance variables are given accessibility), they should be called
using the fully qualified name. ObjectReference.VariableName.
EXAMPLE
This will produce the following
result −
Output
name: Ransika
salary: 1000.0
Class/Static
Variables
Class variables
 also known as static variables are declared with the
static keyword in a class, but outside a method,
constructor or a block.
 There would only be one copy of each class variable
per class, regardless of how many objects are created
from it.
Static variables
 are rarely used other than being declared as constants.
Constants are variables that are declared as
public/private, final, and static. Constant variables
never change from their initial value.
 are stored in the static memory. It is rare to use static
variables other than declared final and used as either
public or private constants.
Class/Static
Variables
Static variables
 are created when the program starts and destroyed when
the program stops.
 Visibility is similar to instance variables. However, most
static variables are declared public since they must be
available for users of the class.
 Default values are same as instance variables. For numbers,
the default value is 0; for Booleans, it is false; and for object
references, it is null. Values can be assigned during the
declaration or within the constructor. Additionally, values
can be assigned in special static initializer blocks.
 Static variables can be accessed by calling with the class
name ClassName.VariableName.
 When declaring class variables as public static final, then
variable names (constants) are all in upper case. If the static
variables are not public and final, the naming syntax is the
same as instance and local variables.
EXAMPLE
This will produce the following result −
Output
Development average salary : 1000
What is
modifiers?
 Modifiers are keywords that you add to those
definitions to change their meanings. Java language has
a wide variety of modifiers, including the following −
 Java Access Modifiers
 Non Access Modifiers
 To use a modifier, you include its keyword in the
definition of a class, method, or variable. The modifier
precedes the rest of the statement, as in the following
example.
EXAMPLE
Note: the italicized words are modifiers
ACCESS
CONTROL
MODIFIERS
 Java provides a number of access modifiers to
set access levels for classes, variables, methods
and constructors. The four access levels are −
 Visible to the package, the default. No
modifiers are needed.
 Visible to the class only (private).
 Visible to the world (public).
 Visible to the package and all subclasses
(protected).
NON -ACCESS
CONTROL
MODIFIERS
 Java provides a number of non-access modifiers
to achieve many other functionality.
 The static modifier for creating class methods
and variables.
 The final modifier for finalizing the
implementations of classes, methods, and
variables.
 The abstract modifier for creating abstract
classes and methods.
 The synchronized and volatile modifiers, which
are used for threads.
Java Variables, Modifiers and Types in 40 Characters

More Related Content

What's hot

What's hot (20)

Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Java Chapter 04 - Writing Classes: part 2
Java Chapter 04 - Writing Classes: part 2Java Chapter 04 - Writing Classes: part 2
Java Chapter 04 - Writing Classes: part 2
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Java Chapter 04 - Writing Classes: part 1
Java Chapter 04 - Writing Classes: part 1Java Chapter 04 - Writing Classes: part 1
Java Chapter 04 - Writing Classes: part 1
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
javainheritance
javainheritancejavainheritance
javainheritance
 
java
java java
java
 
Abap Objects for BW
Abap Objects for BWAbap Objects for BW
Abap Objects for BW
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 

Similar to Java Variables, Modifiers and Types in 40 Characters

OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulationİbrahim Kürce
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Chapter4-var.pdf
Chapter4-var.pdfChapter4-var.pdf
Chapter4-var.pdfkumari36
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitishChaulagai
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics PresentationOmid Sohrabi
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxIndu65
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHPmtoppa
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Variables in java.pptx
Variables in java.pptxVariables in java.pptx
Variables in java.pptxgomathikalai
 

Similar to Java Variables, Modifiers and Types in 40 Characters (20)

Core java by amit
Core java by amitCore java by amit
Core java by amit
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
Chapter4-var.pdf
Chapter4-var.pdfChapter4-var.pdf
Chapter4-var.pdf
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Oop
OopOop
Oop
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Java session2
Java session2Java session2
Java session2
 
Variables in java.pptx
Variables in java.pptxVariables in java.pptx
Variables in java.pptx
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

Java Variables, Modifiers and Types in 40 Characters

  • 1. Computer Programming 2 Lesson 6– JAVAVARIABLESAND MODIFIERS Prepared by: Analyn G. Regaton
  • 2. JAVA VARIABLE  A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.  You must declare all variables before they can be used. Following is the basic form of a variable declaration − Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list. Data type variable [ = value], variable [ = value] ,,,];
  • 3. Following are valid examples of variable declaration and initialization in Java This lesson will explain various variable types available in Java Language. There are three kinds of variables in Java −  Local variables  Instance variables  Class/Static variables
  • 4. LOCAL VARIABLES  Local variables  are declared in methods, constructors, or blocks.  are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.  Access modifiers cannot be used for local variables.  Local variables  are visible only within the declared method, constructor, or block.  are implemented at stack level internally.  There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.
  • 5. EXAMPLE  Here, age is a local variable. This is defined inside pupAge() method and its scope is limited to only this method. This will produce the following result − Output Puppy age is : 7
  • 6. ANOTHER EXAMPLE Following example uses age without initializing it, so it would give an error at the time of compilation. This will produce the following error while compiling it − Output
  • 7. INSTANCE VARIABLE Instance variables  are declared in a class, but outside a method, constructor or any block.  When a space is allocated for an object in the heap, a slot for each instance variable value is created.  are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.  hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class  can be declared in class level before or after use.
  • 8. INSTANCE VARIABLE  Access modifiers can be given for instance variables.  The instance variables are visible for all methods, constructors and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers.  Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor.  Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference.VariableName.
  • 9. EXAMPLE This will produce the following result − Output name: Ransika salary: 1000.0
  • 10. Class/Static Variables Class variables  also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.  There would only be one copy of each class variable per class, regardless of how many objects are created from it. Static variables  are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.  are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.
  • 11. Class/Static Variables Static variables  are created when the program starts and destroyed when the program stops.  Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.  Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.  Static variables can be accessed by calling with the class name ClassName.VariableName.  When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.
  • 12. EXAMPLE This will produce the following result − Output Development average salary : 1000
  • 13. What is modifiers?  Modifiers are keywords that you add to those definitions to change their meanings. Java language has a wide variety of modifiers, including the following −  Java Access Modifiers  Non Access Modifiers  To use a modifier, you include its keyword in the definition of a class, method, or variable. The modifier precedes the rest of the statement, as in the following example.
  • 14. EXAMPLE Note: the italicized words are modifiers
  • 15. ACCESS CONTROL MODIFIERS  Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are −  Visible to the package, the default. No modifiers are needed.  Visible to the class only (private).  Visible to the world (public).  Visible to the package and all subclasses (protected).
  • 16. NON -ACCESS CONTROL MODIFIERS  Java provides a number of non-access modifiers to achieve many other functionality.  The static modifier for creating class methods and variables.  The final modifier for finalizing the implementations of classes, methods, and variables.  The abstract modifier for creating abstract classes and methods.  The synchronized and volatile modifiers, which are used for threads.