0% found this document useful (0 votes)
4 views10 pages

JavaTutorial 41 50

The document provides an overview of Java class/static variables, access modifiers, and non-access modifiers, detailing their usage and visibility. It explains how static variables are declared, their memory storage, and how access modifiers (public, private, protected, and default) control visibility in Java. Additionally, it introduces Java's basic operators, including arithmetic, relational, and bitwise operators, with examples demonstrating their functionality.

Uploaded by

amitbod021998
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

JavaTutorial 41 50

The document provides an overview of Java class/static variables, access modifiers, and non-access modifiers, detailing their usage and visibility. It explains how static variables are declared, their memory storage, and how access modifiers (public, private, protected, and default) control visibility in Java. Additionally, it introduces Java's basic operators, including arithmetic, relational, and bitwise operators, with examples demonstrating their functionality.

Uploaded by

amitbod021998
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

salary :1000.

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.

 Static variables are stored in static memory. It is rare to use static variables other than declared final and used
as either public or private constants.

 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 . [Link].

 When declaring class variables as public static final, then variables 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:
import [Link].*;

public class Employee{


// salary variable is a private static variable
private static double salary;

// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";

public static void main(String args[]){


salary = 1000;
[Link](DEPARTMENT+"average salary:"+salary);
}
}

This would produce the following result:

Development average salary:1000

Note: If the variables are access from an outside class the constant should be accessed as
[Link]

TUTORIALS POINT
Simply Easy Learning
What is Next?
You already have used access modifiers ( public & private ) in this chapter. The next chapter will explain you
Access Modifiers and Non Access Modifiers in detail.

TUTORIALS POINT
Simply Easy Learning
7
CHAPTER

Java Modifier Types

M odifiers arekeywords that you add to those definitions to change their meanings. The Java language

has a wide variety of modifiers, including the following:

1. Java Access 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).

Default Access Modifier - No keyword:


Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.

A variable or method declared without any access control modifier is available to any other class in the same
package. The fields in an interface are implicitly public static final and the methods in an interface are by default
public

Example:
Variables and methods can be declared without any modifiers, as in the following examples:

String version ="1.5.1";

boolean processOrder(){
return true;
}

TUTORIALS POINT
Simply Easy Learning
Private Access Modifier - private:
Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

Variables that are declared private can be accessed outside the class if public getter methods are present in the
class.

Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world.

Example:
The following class uses private access control:

public class Logger{


private String format;
public String getFormat(){
return [Link];
}
public void setFormat(String format){
[Link] = format;
}
}

Here, the format variable of the Logger class is private, so there's no way for other classes to retrieve or set its
value directly.
So to make this variable available to the outside world, we defined two public methods: getFormat(), which returns
the value of format, and setFormat(String), which sets its value.

Public Access Modifier - public:


A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields,
methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

However if the public class we are trying to access is in a different package, then the public class still need to be
imported.

Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.

Example:
The following function uses public access control:

public static void main(String[] arguments){


// ...
}

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such
as java) to run the class.

Protected Access Modifier - protected:


Variables, methods and constructors which are declared protected in a superclass can be accessed only by the
subclasses in other package or any class within the package of the protected members' class.

TUTORIALS POINT
Simply Easy Learning
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected,
however methods and fields in a interface cannot be declared protected.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated
class from trying to use it.

Example:
The following parent class uses protected access control, to allow its child class overrideopenSpeaker() method:

class AudioPlayer{
protected boolean openSpeaker(Speaker sp){
// implementation details
}
}

class StreamingAudioPlayer{
boolean openSpeaker(Speaker sp){
// implementation details
}
}

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other
than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our
intension is to expose this method to its subclass only, thats why we usedprotected modifier.

Access Control and Inheritance:


The following rules for inherited methods are enforced:

 Methods declared public in a superclass also must be public in all subclasses.

 Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be
private.

 Methods declared without access control (no modifier was used) can be declared more private in subclasses.

 Methods declared private are not inherited at all, so there is no rule for them.

2. 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 examples (Italic ones):

public class className {


// ...
}
private boolean myFlag;
static final double weeks =9.5;
protected static final int BOXWIDTH =42;
public static void main(String[] arguments){
// body of method
}

TUTORIALS POINT
Simply Easy Learning
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 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.
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 examples (Italic ones):

publicclass className {
// ...
}
private boolean myFlag;
static final double weeks =9.5;
protected static final int BOXWIDTH =42;
public static void main(String[] arguments){
// body of method
}

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 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.

TUTORIALS POINT
Simply Easy Learning
 The synchronized and volatile modifiers, which are used for threads.

What is Next?
In the next section, I will be discussing about Basic Operators used in the Java Language. The chapter will give you
an overview of how these operators can be used during application development.

TUTORIALS POINT
Simply Easy Learning
8
CHAPTER

Java Basic Operators

J ava provides a rich set of operators to manipulate variables. We can divide all the Java operators into the

following groups:

 Arithmetic Operators

 Relational Operators

 Bitwise Operators

 Logical Operators

 Assignment Operators

 Misc Operators

The Arithmetic Operators:


Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The
following table lists the arithmetic operators:
Assume integer variable A holds 10 and variable B holds 20, then:

Operator Description Example

+ Addition - Adds values on either side of the operator A + B will give 30

- Subtraction - Subtracts right hand operand from left hand operand A - B will give -10

* Multiplication - Multiplies values on either side of the operator A * B will give 200

/ Division - Divides left hand operand by right hand operand B / A will give 2

Modulus - Divides left hand operand by right hand operand and returns
% B % A will give 0
remainder

++ Increment - Increases the value of operand by 1 B++ gives 21

-- Decrement - Decreases the value of operand by 1 B-- gives 19

TUTORIALS POINT
Simply Easy Learning
Example
The following simple example program demonstrates the arithmetic operators. Copy and paste the following Java
program in [Link] file and compile and run this program:

public class Test{

public static void main(String args[]){


int a =10;
int b =20;
int c =25;
int d =25;
[Link]("a + b = "+(a + b));
[Link]("a - b = "+(a - b));
[Link]("a * b = "+(a * b));
[Link]("b / a = "+(b / a));
[Link]("b % a = "+(b % a));
[Link]("c % a = "+(c % a));
[Link]("a++ = "+(a++));
[Link]("b-- = "+(a--));
// Check the difference in d++ and ++d
[Link]("d++ = "+(d++));
[Link]("++d = "+(++d));
}
}

This would produce the following result:

a + b =30
a - b =-10
a * b =200
b / a =2
b % a =0
c % a =5
a++=10
b--=11
d++=25
++d =27

The Relational Operators:


There are following relational operators supported by Java language:
Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example

Checks if the values of two operands are equal or not, if yes then
== (A == B) is not true.
condition becomes true.

Checks if the values of two operands are equal or not, if values are not
!= (A != B) is true.
equal then condition becomes true.

Checks if the value of left operand is greater than the value of right
> (A > B) is not true.
operand, if yes then condition becomes true.

Checks if the value of left operand is less than the value of right
< (A < B) is true.
operand, if yes then condition becomes true.

Checks if the value of left operand is greater than or equal to the value
>= (A >= B) is not true.
of right operand, if yes then condition becomes true.

TUTORIALS POINT
Simply Easy Learning
Checks if the value of left operand is less than or equal to the value of
<= (A <= B) is true.
right operand, if yes then condition becomes true.

Example
The following simple example program demonstrates the relational operators. Copy and paste the following Java
program in [Link] file and compile and run this program. :

public class Test{

public static void main(String args[]){


int a =10;
int b =20;
[Link]("a == b = "+(a == b));
[Link]("a != b = "+(a != b));
[Link]("a > b = "+(a > b));
[Link]("a < b = "+(a < b));
[Link]("b >= a = "+(b >= a));
[Link]("b <= a = "+(b <= a));
}
}

This would produce the following result:

a == b =false
a != b =true
a > b =false
a < b =true
b >= a =true
b <= a =false

The Bitwise Operators:


Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise operator works on bits and performsbit-by-bit operation. Assume if a = 60; and b = 13; now in binary format
they will be as follows:

a = 0011 1100

b = 0000 1101

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

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

The following table lists the bitwise operators:

Assume integer variable A holds 60 and variable B holds 13, then:

TUTORIALS POINT
Simply Easy Learning

You might also like