0% found this document useful (0 votes)
5 views

2 Java Core

Uploaded by

Rouahmed17
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2 Java Core

Uploaded by

Rouahmed17
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

CLASS_OBJECTS

A class is a blueprint/template that defines the state and behavior of objects. It is the logical entity that wraps all
our code into a single unit and structures the code. In Java, we cannot compile our code without a class. Hence,
we must write the code only within the class body surrounded by Curly braces.

The unique characteristic of a class is that a user can define their data members (Variables with Data Types) and
methods, and we can access them by creating instances of the class

Different ways to create an object in java


We can create objects in different ways apart from using the standard way by using the new keyword. There are 4
ways to create objects:

Using newInstance() method of Class

Using newInstance() method of Constructor Class.

Using clone() method

By Deserialization.

What is the difference between Class and Object in Java?


Now, let us have a look at the key differences between Class and Object in java.

Class Object

1. It is a blueprint or template for creating objects. 1. It is an instance of a class which has a state and behavior.

2. A Class characterizes a group of similar objects 2. An object represents a real world entity with some attributes and
having same properties. features.

3. In java, Class is only loaded in memory during 3. On the other hand, Objects are allocated memory dynamically as they
compile time for reference. are created during runtime .

4. A Class represents a logical entity which binds data 4. An Object is a physical entity which provides us means to access the
and methods i.e. our code into a single unit. members of a class.

5. A Class is declared only once. We declare a class 5. Objects can be created more than once depending on the requirement.
using keyword class. We declare objects using new Keyword.

6. Whereas, we can create objects in multiple ways like: Using


6. We can define a class in only one way.
newInstance() method, Using clone() method etc.
VARIABLE

What are variables in java?


In Java, variables are nothing but a named memory location. Variables in java stores values of elements while the
program executes. All operations done on the variable affects the memory location.

Simply put, Variables are helpful to programmers to handle data that can be manipulated by the program during
execution as well. Variables in java are Strongly typed; hence they all must have a datatype followed by an
identifier.

Variable has a name that is used to identify it. The name of the variable is known as the variable’s identifier.

There are some naming conventions that you need to follow while declaring a variable.

 You can use A to Z or a to z.

 You can use 0 to 9.

 You can use special symbol$ and _ to declare a variable.

 You can not start a variable name with number.

 You can not have spaces in variable name.

 You can not use reserved keywords in variable name.

 Variables names are case sensitive

What are the different types of variables in java?


Local variable
A variable declared inside a method or a block is termed a Local Variable. In Java, Local Variables can also be
declared in a block within a method surrounded by curly braces; such variables are known as Block Level
Local Variables.
In such a case, the scope of the variable, both in terms of its access and the presence of the variable in the memory,
will be valid throughout the block only and not through the entire program. We will understand this with an
example.

Note: It is mandatory to initialize the local variables; otherwise, the compiler will throw an error about it.
Moreover, we cannot have Keywords like public, private, static associated with Local Variables.

Instance variable
A variable declared at the class level but outside a method or a block is an Instance variable. It is not mandatory to
initialize instance variables. Instead, an Instance variable in Java is initialized when an instance of the class or
an object is created.

All instance variables will be, by default, initialized by JVM. While allocating space for an object, a slot for each
Instance Variable is also created. Thus, we can say Instance Variables are present in memory until the object’s
lifetime and are visible to all methods and blocks in the class.

We can use Keywords and Access Modifiers with Instance Variables to restrict the visibility of the variables. Let us
understand the use of instance variable with example.
To access the value in the main method, we had to create the object or instance of the Class InstanceDemo; then,
we used the dot operator with the object name to access the variable.

If we had accessed it directly without creating the object, the compiler would throw an error: non-static variable g
cannot be referenced from a static context. This error means that the main method is static, and we cannot access a
non-static variable from a static method. It is mandatory to instantiate the class in this case.

Static variable
A variable that is declared as static and refers to shared property for all class objects is known as a Static variable.
Static variables are also class-level variables that can be declared in a method but only in a Static Method.
We cannot declare static variables inside a Static Block.

Static Variables in Java make the program memory efficient as static variables get memory allocation only once
during class loading. In simple words, all objects of a class will share the same static variable or properties.

Even if we create multiple objects or instances, static members are created only once. The static keyword denotes
that a property belongs to the whole class rather than a method. Like Instance variables, Static members are also by
default initialized by JVM.

Use Cases for different Variables in Java


Let’s assume we are trying to store information related to a Student object. Now, we know that each Student’s
names and roll number are unique to them and are different, so we can use Instance Variables to store that kind of
information.

Suppose we want to store information that will be the same for all Student Objects like their School Name, so in that
case, we can use a Static variable so that every object shares the same variable. If we want to temporarily store any
value related to the object for any operations, we can use Local Variables.
keyword

Final variable
If you make any variable final then you are not allowed to change its value later.It will be constant.If you try to
change value, then compiler will give you error.

Blank final variable


is the variable which is not initialized at the time of declaration. It can be initialized only in constructor. But if you do
not initialize final variable, you will get compilation error as below.

So you can declare it final and initialize it in constructor.

static blank final variable


static blank final variable is static variable which is not initialized at the time of declaration. It can be initialized only
in static block.
Final method
You can not override final methods in subclasses. You can call parent’s class final method using subclass’s object but
you can not override it.

Final Class
If you declare a class final, no other class can extend it.
Static keyword
 Variable
 Method
 Block
 Nested class

Static variable:
 Static variable belongs to a class not to a object.
 Static variable are initialized only once in class area at the time of class loading
 All objects share single copy of static variable
 You don’t need to create object to access static variable.You can directly access it using class name.
 Static variable can be accessed by instance java methods also.

If you create a class with two variables, one static, one non static.Non static variable of that class (objects created
from that class) gets its own version of that variable. But with the static variable, it belongs to the class, and there is
only one. Although any object from that class can reference it.

Static Method:

 Static method belongs to a class not to object.


 Static method can be invoked directly using a className.Although It can be invoked using objectName
also
 Generally static method are used for getting static fields.
 You can access static methods through java instance methods.
 You can not access non static method or instance variable in static methods.
 A static method can not refer to this or super keyword.

Static block:
The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the
JVM

 Static blocks are used to initialize static data members.


 Static blocks get executed before main method get executed.

Static class:
In java,you can define a class within another class. Such a class is called a nested class. The class which enclosed
nested class is known as Outer class. In java, we can’t make outer class static.

Only nested class can be declared as static

 Nested static class does not need reference of outer class.


 A static class cannot access non-static members of the Outer class. It can access only static members of
Outer class including private.
Data Types in Java Programming

What is the difference between Primitive and Non-Primitive


Data Types?
Now let’s have a quick look at the key differences between Primitive and Non-primitive
or Reference Data Types.
Primitive Data Types Non-Primitive or Reference Data Types

1. It is the built-in Data Type that is predefined by Java 1. It is the user-defined datatypes and not predefined by
itself. Java except the String Class.

2. Java allocates different sizes or bytes for each Primitive


2. Java allocates fixed size (8 bytes) for each Non-Primitive
Data Type. The size of each Primitive Data Type differs
Data Type.
with their type.

3. It does not provide any inbuilt methods to invoke for 3. We can invoke or call methods, even define our own
performing operations on the data. methods to perform operations.

4. It always has a value to it and cannot be null. 4. It can have a null value.

5. As a reference data type, it must begin with Uppercase


5. It begins with a lowercase letter. Examples: int, char, Letter because all classes in Java follow the same
float, double, etc. convention. Examples: Array, String, Object Class, and all
User-defined Classes.

6. While creating a Primitive Data Type, we assign a value


6. Here, we assign a reference to the variable.
to it.

Access modifiers in java


The access modifiers in java define accessibility (scope) of variable, method, constructor or class.
There are 4 types of access modifiers in java.

Public access modifier


Public modifier is accessible in the whole java world. If you put class as the public that means that class is
available everywhere

Private access modifier


The private access modifier is accessible only within class.
You can not use private and protected with class unless and until it is nested class.

Default access modifier


If you do not provide any access, JVM considers it as default access. In the case of default access modifier,

you can not access method, variable or class outside of the package.

Protected access modifier


Protected access modifiers can be accessed within the same package or outside the package by inheritance only.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy