Jbad 3
Jbad 3
The static keyword belongs to the class than an instance of the class.
The static variable can be used to refer to the common property of all
objects (which is not unique for each object), for example, the company
name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time of
class loading.
class Student{
int rollno;
String name;
String college="ITS";
}
Suppose there are 500 students in my college, now all instance data
members will get memory each time when the object is created.
All students have its unique rollno and name, so instance data member
is good in such case.
Here, "college" refers to the common property of all objects.
If we make it static, this field will get the memory only once.
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
} }
class Counter
{
int count=0;//will get memory each time when the instance is created
Counter()
{
count++;//incrementing value
System.out.println(count);
}
public static void main(String args[])
{
//Creating objects
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
The static method can not use non static data member or call
non-static method directly.
class A
{
int a=40;//non static
public static void main(String args[])
{
System.out.println(a);
}
}
O/P :: Compile Time Error
1. Local Variables
A variable defined within a block or method or constructor is called a
local variable.
These variables are created when the block is entered, or the
function is called 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.
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.
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.
this keyword
It is a reserved keyword in Java that is used to refer to the current class
object.
this() Constructor
The constructor is used to call one constructor from the other of the same
class.
Let's take an example of both this keyword and this() to understand how
they work.
ThisExample1.java
// import required classes and packages
class ThisExample1 {
ThisExample1(int x)
{
this.x = x; // override value of the current class instance variable
System.out.println("We are inside of the parameterized constructo
r.");
System.out.println("The value of y = "+y);
}
public static void main(String[] args)
{
// invoking constructor of the current class
new ThisExample1();
System.out.println("Inside Main");
}
}
Output:
super Keyword
A reserved keyword used to call the base class method or variable is
known as a super keyword. We cannot use the super keyword as
an identifier. The super keyword is not only used to refer to the base
class instance but also static members too.
super() Constructor
The super() is mainly used for invoking base class member functions
and constructors.
Let's take an example of both the super keyword and super() to understand
how they work.
SuperExample1.java
}
// create child class of Animal
class Cat extends Animal{
//default constructor
Cat()
{
// data members of the Cat class
String color = "Brown";
System.out.println("The cat is of color "+super.color); // calling parent class
data member
System.out.println("The cat is of color "+color);
}
}
// create child class for Car
class SuperExample1 extends Cat
{
// default constructor
SuperExample1()
{
// calling base class constructor
super();
System.out.println("The eyes of the cat is blue.");
}
// main() method start
public static void main(String[] args)
{
// call default constructor of the SuperExample1
new SuperExample1();
System.out.println("Inside Main");
}
}
int c = 3;
int d = c++; // Now, c is 4, but d is 3
Here, d is assigned the current value of c
before c is incremented.
int y = 8;
--y; // Equivalent to y = y - 1;546yhb `
Both of these statements decrease the
value of y by 1.
int m = 7;
int n = --m; // Now, m is 6 and n is also 6
ii. Postfix Decrement Operator:
-- type var-name[];
-- type[] var-name;
The element type determines the data type of each element that
comprises the array.
MyClass myClassArray[];
// array of Object
Object[] ao,
// array of Collection
// of unknown type
Collection[] ca;
//declaring array
int intArray[];
// allocating memory to array
intArray = new int[20];
// combining both statements in one
int[] intArray = new int[20];
Note: The elements in the array allocated by new will
automatically be initialized to zero (for numeric types), false (for
boolean), or null (for reference types).
Obtaining an array is a two-step process.
First, you must declare a variable of the desired array type.
Second, you must allocate the memory to hold the array, using
new, and assign it to the array variable.
Thus, in Java, all arrays are dynamically allocated.
Each element in the array is accessed via its index. The index
begins with 0 and ends at (total array size)-1.
All the elements of array can be accessed using Java for Loop.
class Demo {
public static void main (String[] args) {
int [] arr=new int [4];
// 4 is the size of arr
System.out.println("Array Size:"+arr.length);
}
}
The student Array contains five memory spaces each of the size of
student class in which the address of five Student objects can be stored.
class Student {
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
// so on...
arr[2] = new Student(3, "shikar");
arr[3] = new Student(4, "dharmesh");
arr[4] = new Student(5, "mohit");
// Driver class
class Demo {
public static void main(String[] args)
{
// Syntax
int[][] arr = new int[3][3];
// 3 row and 3 column
arr[][]= { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };
// printing 2D array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
System.out.print(arr[i][j] + " ");
}
System.out.println();
// Number of Rows
System.out.println("Number of Rows:"+
arr.length);
// Number of Columns
System.out.println("Number of Columns:"+
arr[0].length);
}
}