[go: up one dir, main page]

0% found this document useful (0 votes)
10 views31 pages

Php Unit 1 (1)

The document provides an overview of object-oriented programming (OOP) concepts, focusing on Java programming basics, including data types, control structures, and core OOP principles like classes, objects, inheritance, and polymorphism. It explains Java syntax, the structure of a Java program, and the compilation and execution process, along with details on primitive and non-primitive data types. Key elements such as encapsulation, data abstraction, and message passing are also discussed, highlighting their significance in OOP.

Uploaded by

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

Php Unit 1 (1)

The document provides an overview of object-oriented programming (OOP) concepts, focusing on Java programming basics, including data types, control structures, and core OOP principles like classes, objects, inheritance, and polymorphism. It explains Java syntax, the structure of a Java program, and the compilation and execution process, along with details on primitive and non-primitive data types. Key elements such as encapsulation, data abstraction, and message passing are also discussed, highlighting their significance in OOP.

Uploaded by

divyashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Unit-1 : Object oriented concepts and paradigm, Basics

of Java programming, Data types, Variables, Operators,


Control structures including selection, Looping, method
Overloading, Math class, Arrays in java. Objects and
Classes: Basics of objects and classes in java,
Constructors, Finalizer, Visibility modifiers, Methods and
objects, Inbuilt classes like String, Character, String
Buffer, File, this reference, I/O streams.
Introduction of Object Oriented Programming
As the name suggests, Object-Oriented Programming or OOPs
refers to languages that use objects in programming. Object-
oriented programming aims to implement real-world entities
like inheritance, hiding, polymorphism, etc in programming. The
main aim of OOP is to bind together the data and the functions
that operate on them so that no other part of the code can
access this data except that function.
OOPs Concepts:
 Class
 Objects
 Data Abstraction
 Encapsulation
 Inheritance
 Polymorphism
 Dynamic Binding
 Message Passing
1. Class:
A class is a user-defined data type. It consists of data members
and member functions, which can be accessed and used by
creating an instance of that class. It represents the set of
properties or methods that are common to all objects of one
type. A class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many
cars with different names and brands but all of them will share
some common properties like all of them will have 4 wheels,
Speed Limit, Mileage range, etc. So here, Car is the class, and
wheels, speed limits, mileage are their properties.
2. Object:
It is a basic unit of Object-Oriented Programming and
represents the real-life entities. An Object is an instance of a
Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is
allocated. An object has an identity, state, and behavior. Each
object contains data and code to manipulate the data. Objects
can interact without having to know details of each other’s data
or code, it is sufficient to know the type of message accepted
and type of response returned by the objects.
For example “Dog” is a real-life Object, which has some
characteristics like color, Breed, Bark, Sleep, and Eats.

Object
3. Data Abstraction:
Data abstraction is one of the most essential and important
features of object-oriented programming. Data abstraction
refers to providing only essential information about the data to
the outside world, hiding the background details or
implementation. Consider a real-life example of a man driving a
car. The man only knows that pressing the accelerators will
increase the speed of the car or applying brakes will stop the
car, but he does not know about how on pressing the
accelerator the speed is increasing, he does not know about the
inner mechanism of the car or the implementation of the
accelerator, brakes, etc in the car. This is what abstraction is.

4. Encapsulation:
Encapsulation is defined as the wrapping up of data under a
single unit. It is the mechanism that binds together code and
the data it manipulates. In Encapsulation, the variables or data
of a class are hidden from any other class and can be accessed
only through any member function of their class in which they
are declared. As in encapsulation, the data in a class is hidden
from other classes, so it is also known as data-hiding.

Consider a real-life example of encapsulation, in a company,


there are different sections like the accounts section, finance
section, sales section, etc. The finance section handles all the
financial transactions and keeps records of all the data related
to finance. Similarly, the sales section handles all the sales-
related activities and keeps records of all the sales. Now there
may arise a situation when for some reason an official from the
finance section needs all the data about sales in a particular
month. In this case, he is not allowed to directly access the
data of the sales section. He will first have to contact some
other officer in the sales section and then request him to give
the particular data. This is what encapsulation is. Here the data
of the sales section and the employees that can manipulate
them are wrapped under a single name “sales section”.
5. Inheritance:
Inheritance is an important pillar of OOP(Object-Oriented
Programming). The capability of a class to derive properties and
characteristics from another class is called Inheritance. When
we write a class, we inherit properties from other classes. So
when we create a class, we do not need to write all the
properties and functions again and again, as these can be
inherited from another class that possesses it. Inheritance
allows the user to reuse the code whenever possible and
reduce its redundancy.

6. Polymorphism:
The word polymorphism means having many forms. In simple
words, we can define polymorphism as the ability of a message
to be displayed in more than one form. For example, A person
at the same time can have different characteristics. Like a man
at the same time is a father, a husband, an employee. So the
same person posses different behavior in different situations.
This is called polymorphism.
7. Dynamic Binding:
In dynamic binding, the code to be executed in response to the
function call is decided at runtime. Dynamic binding means that
the code associated with a given procedure call is not known
until the time of the call at run time. Dynamic Method Binding
One of the main advantages of inheritance is that some derived
class D has all the members of its base class B. Once D is not
hiding any of the public members of B, then an object of D can
represent B in any context where a B could be used. This
feature is known as subtype polymorphism.
8. Message Passing:
It is a form of communication used in object-oriented
programming as well as parallel programming. Objects
communicate with one another by sending and receiving
information to each other. A message for an object is a request
for execution of a procedure and therefore will invoke a function
in the receiving object that generates the desired results.
Message passing involves specifying the name of the object,
the name of the function, and the information to be sent.
Java Syntax
Java is an object-oriented programming language which is
known for its simplicity, portability, and robustness. The syntax
of Java programming language is very closely aligned with C
and C++ which makes it easier to understand.
Java Syntax refers to a set of rules that defines how Java
programs are written and interpreted by the compiler. These
rules ensure that your code is readable, logically correct, and
error-free as well. Now, Let’s understand the Syntax and
Structure of Java Programs with a basic “Hello World” program.
Structure of Java Program
A basic Java program consists of several components that
create a functional application. W:e can learn about basic Java
Syntax using the following program:
// FileName : "Test.java".

public class Test{

// Program begins with a call to main() method


// main method is the entry point of a Java Program
public static void main(String args[])
{

// Prints "Hello World" to the console


System.out.println("Hello World");
}
}

Output
Hello World
Explanation: The above program shows the basic Java
program that contains Class declaration, Main Method,
Statements, etc. Let’s try to understand them one by one.
Terminologies of a Basic Java Program
1. Class Declaration: Every Java program starts with a class
declaration using the class keyword. “A class is a blue print
of an object” and we can also define class as a logical
template that share common properties and methods.
2. Object: The object is an instance of a class. It is an entity
that has behavior and state.
 Example: Dog, Cat, Monkey etc. are the object of “Animal”
class.
 Behavior: Running on the road.
3. Main Method: The public static void main(String
args[]) method is the entry point where the program starts
execution.
4. Statements: Each line of code inside the method must end
with a semicolon(;)
Steps to Compile and Run a Java Program in a Console
1. Run the javac command to compile the Java Program
javac Test.java
The java compiler(javac) reads the source code(Test.java) and
generates a bytecode file(Test.class) in the same directory
2. Use the java command to execute the complied
bytecode
java Test
Note:
 Do include the .class extension in the command.
 Make sure Test.class file is in the current directory.
Java Syntax Key Elements
1. Comments in Java
There are three types of comments in Java.
 Single line Comment
// This is a single-line comment
 Multi-line Comment
/* This is
a multi-line comment */
 Documentation Comment
It is also known as doc comment
/** This is a doc comment */
2. Source File Name
Source File Name Rule in Java
1. When There is a public class in the file
The name of a source file must exactly match the name of the
public class name with the extension of . java
Example: Assume you have a public class name GFG.
import java.io.*;

public class GFG {


public static void main (String[] args) {
System.out.println("Hello World!");
}
}
Note:
 GFG. java this is a Valid Syntax
 gfg.java this is a Invalid Syntax
2. When There is no public class in the file
The source file name can be anything, but it must have
the .java extension.
class Program {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Here, the class in not declared as public so you can make the
file name anything like Example.java, Program.java etc
3. Case Sensitivity
Java is a case-sensitive language, which means that the
identifiers AB, Ab, aB , and ab are different in Java.
System.out.println(“Hello World”); // valid syntax
system.out.println(“Hello World”); // invalid syntax because of
the first letter of System keyword is always uppercase.
4. Class Names
 The first letter of the class should be in Uppercase
(lowercase is allowed but discouraged).
 If several words are used to form the name of the class,
each inner word’s first letter should be in Uppercase.
Underscores are allowed, but not recommended. Also
allowed are numbers and currency symbols, although the
latter are also discouraged because they are used for a
special purpose (for inner and anonymous classes).
class MyJavaProgram // valid syntax
class 1Program // invalid syntax
class My1Program // valid syntax
class $Program // valid syntax, but discouraged
class My$Program // valid syntax, but discouraged (inner class
Program inside the class My)
class myJavaProgram // valid syntax, but discouraged

Java Data Types


Java is statically typed and also a strongly typed language
because, in Java, each type of data (such as integer, character,
hexadecimal, packed decimal, and so forth) is predefined as
part of the programming language and all constants or
variables defined for a given program must be described with
one of the Java data types.
Data types in Java are of different sizes and values that can
be stored in the variable that is made as per convenience and
circumstances to cover up all test cases. Java has two
categories in which data types are segregated
1. Primitive Data Type: such as boolean, char, int, short,
byte, long, float, and double. The Boolean with uppercase
B is a wrapper class for the primitive data type boolean in
Java.
2. Non-Primitive Data Type or Object Data type: such
as String, Array, etc.
Example:
// Java Program to demonstrate int data-type
import java.io.*;

class GFG
{
public static void main (String[] args)
{
// declaring two int variables
int a = 10;
int b = 20;

System.out.println( a + b );
}
}
Output
30
Now, let us explore different types of primitive and non-
primitive data types.

Data Types in JAVA


Primitive Data Types in Java
Primitive data are only single values and have no special
capabilities. There are 8 primitive data types. They are
depicted below in tabular format below as follows:
Examp
le
Descripti Defau Siz Literal Range of
Type on lt e s values

boolea true or 8 true,


false true, false
n false bits false

twos-
8
compleme 0 (none) -128 to 127
bits
byte nt integer

‘a’, ‘\ characters
u0041’, representation
Unicode \ 16 of ASCII
‘\101’,
character u0000 bits values
‘\\’, ‘\’,
char ‘\n’, ‘β’ 0 to 255

twos-
16 -32,768 to
compleme 0 (none)
bits 32,767
short nt integer

-
twos- 2,147,483,648
32 -2,-
compleme 0
bits 1,0,1,2 to
nt intger
int 2,147,483,647

long twos- 0 64 -2L,- -


compleme bits 1L,0L,1 9,223,372,036
nt integer L,2L ,854,775,808
to
9,223,372,036
Examp
le
Descripti Defau Siz Literal Range of
Type on lt e s values

,854,775,807

1.23e1
00f , -
IEEE 754
32 1.23e- upto 7
floating 0.0
bits 100f , . decimal digits
point
3f ,3.14
float F

1.2345
6e300d
IEEE 754 ,-
64 upto 16
floating 0.0 123456
bits decimal digits
point e-
300d ,
double 1e1d

Primitive Data Types


1. boolean Data Type
The boolean data type represents a logical value that can be
either true or false. Conceptually, it represents a single bit of
information, but the actual size used by the virtual machine is
implementation-dependent and typically at least one byte
(eight bits) in practice. Values of the boolean type are not
implicitly or explicitly converted to any other type using casts.
However, programmers can write conversion code if needed.
Syntax:
boolean booleanVar;
Size : Virtual machine dependent (typically 1 byte, 8 bits)
2. byte Data Type
The byte data type is an 8-bit signed two’s complement integer.
The byte data type is useful for saving memory in large arrays.
Syntax:
byte byteVar;
Size : 1 byte (8 bits)
3. short Data Type
The short data type is a 16-bit signed two’s complement
integer. Similar to byte, a short is used when memory savings
matter, especially in large arrays where space is constrained.
Syntax:
short shortVar;
Size : 2 bytes (16 bits)
4. int Data Type
It is a 32-bit signed two’s complement integer.
Syntax:
int intVar;
Size : 4 bytes ( 32 bits )
Remember: In Java SE 8 and later, we can use the int data
type to represent an unsigned 32-bit integer, which has a value
in the range [0, 2 32 -1]. Use the Integer class to use the int
data type as an unsigned integer.
5. long Data Type
The long data type is a 64-bit signed two’s complement integer.
It is used when an int is not large enough to hold a value,
offering a much broader range.
Syntax:
long longVar;
Size : 8 bytes (64 bits)
Remember: In Java SE 8 and later, you can use the long data
type to represent an unsigned 64-bit long, which has a
minimum value of 0 and a maximum value of 2 64 -1. The Long
class also contains methods like comparing Unsigned, divide
Unsigned, etc to support arithmetic operations for unsigned
long.
6. float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating-
point. Use a float (instead of double) if you need to save
memory in large arrays of floating-point numbers. The size of
the float data type is 4 bytes (32 bits).
Syntax:
float floatVar;
Size : 4 bytes (32 bits)
7. double Data Type
The double data type is a double-precision 64-bit IEEE 754
floating-point. For decimal values, this data type is generally
the default choice. The size of the double data type is 8 bytes
or 64 bits.
Syntax:
double doubleVar;
Size : 8 bytes (64 bits)
Note: Both float and double data types were designed
especially for scientific calculations, where approximation
errors are acceptable. If accuracy is the most prior concern
then, it is recommended not to use these data types and use
BigDecimal class instead.
It is recommended to go through rounding off errors in java.
8. char Data Type
The char data type is a single 16-bit Unicode character with the
size of 2 bytes (16 bits).
Syntax:
char charVar;
Size : 2 bytes (16 bits)
Why is the Size of char 2 bytes in Java?
Unlike languages such as C or C++ that use the ASCII
character set, Java uses the Unicode character set to support
internationalization. Unicode requires more than 8 bits to
represent a wide range of characters from different languages,
including Latin, Greek, Cyrillic, Chinese, Arabic, and more. As a
result, Java uses 2 bytes to store a char, ensuring it can
represent any Unicode character.
Example:
// Java Program to Demonstrate Char Primitive Data Type

class GFG
{
public static void main(String args[])
{

// Creating and initializing custom character


char a = 'G';

// Integer data type is generally


// used for numeric values
int i = 89;

// use byte and short


// if memory is a constraint
byte b = 4;

// this will give error as number is


// larger than byte range
// byte b1 = 7888888955;
short s = 56;

// this will give error as number is


// larger than short range
// short s1 = 87878787878;

// by default fraction value


// is double in java
double d = 4.355453532;

// for float use 'f' as suffix as standard


float f = 4.7333434f;

// need to hold big range of numbers then we need


// this data type
long l = 12121;

System.out.println("char: " + a);


System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("long: " + l);
}
}

Output
char: G
integer: 89
byte: 4
short: 56
float: 4.7333436
double: 4.355453532
long: 12121
Non-Primitive (Reference) Data Types
The Non-Primitive (Reference) Data Types will contain a
memory address of variable values because the reference
types won’t store the variable value directly in memory. They
are strings, objects, arrays, etc.
1. Strings
Strings are defined as an array of characters. The difference
between a character array and a string in Java is, that the string
is designed to hold a sequence of characters in a single
variable whereas, a character array is a collection of separate
char-type entities. Unlike C/C++, Java strings are not
terminated with a null character.
Syntax: Declaring a string
<String_Type> <string_variable> = “<sequence_of_string>”;
Example:
// Declare String without using new operator
String s = "GeeksforGeeks";
// Declare String using new operator
String s1 = new String("GeeksforGeeks");
2. Class
A Class is a user-defined blueprint or prototype from which
objects are created. It represents the set of properties or
methods that are common to all objects of one type. In general,
class declarations can include these components, in order:
1. Modifiers : A class can be public or has default access.
Refer to access specifiers for classes or interfaces in Java
2. Class name: The name should begin with an initial letter
(capitalized by convention).
3. Superclass(if any): The name of the class’s parent
(superclass), if any, preceded by the keyword extends. A
class can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the
keyword implements. A class can implement more than
one interface.
5. Body: The class body is surrounded by braces, { }.
3. Object
An Object is a basic unit of Object-Oriented Programming and
represents real-life entities. A typical Java program creates
many objects, which as you know, interact by invoking
methods. An object consists of :
1. State : It is represented by the attributes of an object. It
also reflects the properties of an object.
2. Behavior : It is represented by the methods of an object.
It also reflects the response of an object to other objects.
3. Identity : It gives a unique name to an object and enables
one object to interact with other objects.
4. Interface
Like a class, an interface can have methods and variables, but
the methods declared in an interface are by default abstract
(only method signature, no body).
 Interfaces specify what a class must do and not how. It is
the blueprint of the class.
 An Interface is about capabilities like a Player may be an
interface and any class implementing Player must be able
to (or must implement) move(). So it specifies a set of
methods that the class has to implement.
 If a class implements an interface and does not provide
method bodies for all functions specified in the interface,
then the class must be declared abstract.
 A Java library example is Comparator Interface . If a class
implements this interface, then it can be used to sort a
collection.
5. Array
An Array is a group of like-typed variables that are referred to
by a common name. Arrays in Java work differently than they
do in C/C++. The following are some important points about
Java arrays.
 In Java, all arrays are dynamically allocated. (discussed
below)
 Since arrays are objects in Java, we can find their length
using member length. This is different from C/C++ where
we find length using size.
 A Java array variable can also be declared like other
variables with [] after the data type.
 The variables in the array are ordered and each has an
index beginning with 0.
 Java array can also be used as a static field, a local
variable, or a method parameter.
 The size of an array must be specified by an int value and
not long or short.
 The direct superclass of an array type is Object.
 Every array type implements the
interfaces Cloneable and java.io.Serializable.
Java Variables
Variables are the containers for storing the data values or you
can also call it a memory location name for the data. Every
variable has a:
 Data Type – The kind of data that it can hold. For
example, int, string, float, char, etc.
 Variable Name – To identify the variable uniquely within
the scope.
 Value – The data assigned to the variable.
There are three types of variables in Java – Local,
Instance, and Static.
Example:
int age = 27; // integer variable having value 27

String name = "gfg" // string variable


How to Declare Java Variables?
We can declare variables in Java as pictorially depicted below:
From the image, it can be easily perceived that while declaring
a variable, we need to take care of two things that are:
datatype: In Java, a data type define the type of data that a
variable can hold.
data_name: Name was given to the variable.In this way, a
name can only be given to a memory location. It can be
assigned values in two ways:
Variable Initialization
Assigning value by taking input
How to Initialize Java Variables?
It can be perceived with the help of 3 components explained above:

Example:
// Declaring float variable
float simpleInterest;

// Declaring and initializing integer variable


int time = 10, speed = 20;

// Declaring and initializing character variable


char var = 'h';
Types of Java Variables
Now let us discuss different types of variables which are listed
as follows:
1. Local Variables
2. Instance Variables
3. Static Variables

Let us discuss the traits of every type of variable listed here in


detail.
1. Local Variables
A variable defined within a block or method or constructor is
called a local variable.
 The Local variable is created at the time of declaration and
destroyed after exiting from the block or when the call
returns from the function.
 The scope of these variables exists only within the block in
which the variables are declared, i.e., we can access these
variables only within that block.
 Initialization of the local variable is mandatory before
using it in the defined scope.
Example 1:
// Java Program to show the use of local variables
import java.io.*;

class GFG {
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10;

// This variable is local to this main method only


System.out.println("Local Variable: " + var);
}
}

Output
Local Variable: 10
Example 2:
// Java Program to show the use of
// Local Variables
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// x is a local variable
int x = 10;

// message is also a local


// variable
String message = "Hello, world!";
System.out.println("x = " + x);
System.out.println("message = " + message);

if (x > 5) {
// result is a
// local variable
String result = "x is greater than 5";
System.out.println(result);
}

// Uncommenting the line below will result in a


// compile-time error System.out.println(result);

for (int i = 0; i < 3; i++) {


String loopMessage
= "Iteration "
+ i; // loopMessage is a local variable
System.out.println(loopMessage);
}

// Uncommenting the line below will result in a


// compile-time error
// System.out.println(loopMessage);
}
}

Output
x = 10
message = Hello, world!
x is greater than 5
Iteration 0
Iteration 1
Iteration 2
2. Instance Variables
Instance variables are non-static variables and are declared in a
class outside of any method, constructor, or block.
 As instance variables are declared in a class, these
variables are created when an object of the class is
created and destroyed when the object is destroyed.
 Unlike local variables, we may use access specifiers for
instance variables. If we do not specify any access
specifier, then the default access specifier will be used.
 Initialization of an instance variable is not mandatory. Its
default value is dependent on the data type of variable.
For String it is null, for float it is 0.0f, for int it is 0, for
Wrapper classes like Integer it is null, etc.
 Scope of instance variables are throughout the class
except the static contexts.
 Instance variables can be accessed only by creating
objects.
 We initialize instance variables using constructors while
creating an object. We can also use instance blocks to
initialize the instance variables.
Example:
// Java Program to show the use of
// Instance Variables
import java.io.*;
class GFG {

// Declared Instance Variable


public String geek;
public int i;
public Integer I;
public GFG()
{
// Default Constructor
// initializing Instance Variable
this.geek = "Shubham Jain";
}

// Main Method
public static void main(String[] args)
{
// Object Creation
GFG name = new GFG();

// Displaying O/P
System.out.println("Geek name is: " + name.geek);
System.out.println("Default value for int is "+ name.i);

// toString() called internally


System.out.println("Default value for Integer is "+ name.I);
}
}
Output
Geek name is: Shubham Jain
Default value for int is 0
Default value for Integer is null
3. Static Variables
Static variables are also known as class variables.
 These variables are declared similarly to instance
variables. The difference is that static variables are
declared using the static keyword within a class outside of
any method, constructor, or block.
 Unlike instance variables, we can only have one copy of a
static variable per class, irrespective of how many objects
we create.
 Static variables are created at the start of program
execution and destroyed automatically when execution
ends.
 Initialization of a static variable is not mandatory. Its
default value is dependent on the data type of variable.
For String it is null, for float it is 0.0f, for int it is 0,
for Wrapper classes like Integer it is null, etc.
 If we access a static variable like an instance variable
(through an object), the compiler will show a warning
message, which won’t halt the program. The compiler will
replace the object name with the class name
automatically.
 If we access a static variable without the class name, the
compiler will automatically append the class name. But for
accessing the static variable of a different class, we must
mention the class name as 2 different classes might have
a static variable with the same name.
 Static variables cannot be declared locally inside an
instance method.
 Static blocks can be used to initialize static variables.
Example:
// Java Program to show the use of
// Static variables
import java.io.*;

class GFG {
// Declared static variable
public static String geek = "Shubham Jain";

public static void main(String[] args)


{

// geek variable can be accessed without object


// creation Displaying O/P GFG.geek --> using the
// static variable
System.out.println("Geek Name is : " + GFG.geek);

// static int c = 0;
// above line, when uncommented,
// will throw an error as static variables cannot be
// declared locally.
}
}

Output
Geek Name is : Shubham Jain
Instance Variables vs Static Variables
Now let us discuss the differences between the Instance
variables and the Static variables:
 Each object will have its own copy of an instance variable,
whereas we can only have one copy of a static variable
per class, irrespective of how many objects we create.
Thus, static variables are good
for memory management.
 Changes made in an instance variable using one object
will not be reflected in other objects as each object has its
own copy of the instance variable. In the case of a static
variable, changes will be reflected in other objects as
static variables are common to all objects of a class.
 We can access instance variables through object
references, and static variables can be accessed directly
using the class name.
 Instance variables are created when an object is created
with the use of the keyword ‘new’ and destroyed when the
object is destroyed. Static variables are created when the
program starts and destroyed when the program stops.
Syntax: Static and instance variables
class GFG
{
// Static variable
static int a;

// Instance variable
int b;
}

You might also like