CoreJava Day 3( Arrays , Static and Non Static )
CoreJava Day 3( Arrays , Static and Non Static )
- HemanthKumar C
Note : If the value is not stored in a particular index and if we try to print , we will get default values.
0 1 2 3 4
class Array{
public static void main ( String [ ] args ) {
int[] arr = new int[3] ; 2D ARRAY / 2 DIMENSIONAL ARRAY
arr [ 0 ] = 10 ;
arr [ 1 ] = 20 ;
arr [ 2 ] = 30 ;
for ( int i=0; i<= arr . length ; i++ )
{
System.out.println(i+"\t"+ arr[i]);
}}}
class Array {
public static void main ( String [ ] args ){
int[] arr = {10,20,30,40};
System.out.println("index\t values");
for ( int i=0; i<= arr . length ; i++ )
{
System.out.println(i+"\t"+ arr[i]);
}}}
In a class , we have 3 members
• variables
• methods
• constructor
• Variables and methods can be classified into static and
non - static.
• Constructor is always non – static
class Tester {
public static void main(String[] args) {
Circle. Area();
} }
NON – STATIC
➢ Any member of the class declared without a keyword static is called as Non - static member of
the class.
➢ Non - Static is always associated with object.
➢ Non - Static is multiple copy.
➢ All the Non - static members will be stored in inside the Heap Memory
➢ Whenever we want to access from Non – static to static then we should use
➢ Object . variable _ name or object . method _ name
➢ Reference variable . variable _ name or Reference variable . method _ name
initialized the constructor and from constructor it will get initialized 4.method area – It is used to store method body or definition . Irrespective of static or non – static all
to the object variable. the method bodies will be stored in method area.
➢ Now in the main method the object address will be stored in the class Circle
references {
variable e1 and through that references variables we point the values. int x=17;
void area(int r)
{
Stack Heap memory final double pi = 3.142;
//int r = 4;
new Circle(); new Circle(); double result = pi * r * r;
System.out.println(result);
}
area() int x=7; int x=7;
Void area() Void area()
}
main() public class Non_Static_method
JVM random memory space {
Class Loader
public static void main(String[] args)
{
// Non – static to static without parameter and return type
main() area() public static void main(String[]args)
new Circle().area(5); // new keyword should be mentioned before writing
class_name.methodName
System.out.println(new Circle().x);
Method area static pool area }
REFERENCE VARIABLE Ex : Tester t1 = new Tester ( ) ;
Reference variable is a special type of variable which is used to Class _ name reference variable operator constructor
store object address. → object
Ex : Class Tester {
Reference variable can hold either null or object address.
Void disp ( ) {
Reference variable declaration
System.out.println("Hii");
Syntax : class _ name reference _ variable ;
}
Ex : int a ;
Tester t1 ;
public static void main(String[ ] args)
{
Tester t1 = new Tester ( );
Reference variable initialization
Syntax : reference _ variable = object ; t1.disp ( ) ;
Ex : t1 = new Tester() ; } }
{
area() int x=18; int x=18; Non_Static_with_ReferenceVariable C1 = new
Void area() Void area()
Non_Static_with_ReferenceVariable();
main()
JVM random memory space C1.area();
Class Loader System.out.println(C1.x);
Note : Whenever we print the reference variable it print address i.e. fully qualified
path.
Fully qualified path means package name.class_name @ hexadecimal no