JAVA Beginners part 12

Java has two memory areas which are Heap and Stack
————————————————–
Objects live in Heap ( Garbage collectible Heap)
Variables and methods live in Stack

Instance variables the variables declared in a class and are associated with an Object.They live on Heap.
Local variables are the one declared in a method which include the method parameters as well.They are live only as long as the method is on the stack.
When you call a method, the method gets pushed on top of the stack frame, which holds the line of code and values of the local variables.The method on top of the stack is always the one that is currently running.In case there is a method foo() which is calling method bar(), then bar is currently running hence that will be on top of the stack.

m1.jpg

Values of an Object Instance variables live inside the Object in the Heap.
If the Instance variable is primitive type then Java makes space for the instance variable based on the primitive type, example int needs 32 bits.
If the Instance variable is of type object
example

cellphone()
public class cellphone
{
private antenna ant = new antenna();
}

When you create a cell phone object , Java will create an object in Heap and when the antenna is created it will be linked to the cellphone object.

t3

duck myduck = new duck();

We have instantiated so many objects before but haven’t talked about this. What does new duck(); do ?
yes it creates an object in heap. But how? duck() looks like a method.

Even though duck() looks like a method it is not. It is called as a constructor. constructor is the code that gets called to create an object.constructor gets called when new keyword is used. JVM finds the class and invokes the constructor of that class. Every class has a constructor even if you dont write it.

wait a minute i haven’t written the code, so who did it and how is it available to me?
Well, The compiler writes one for you.

Compilers default constructor will look like this

public duck()
{
}
See there is no return type which is present for methods. The name is same as the class and that is mandatory.

Constructor runs before the object gets assigned to the reference. Which means we can get in middle of it and make some changes if needed.

Constructor is used to initialize the state of an Object which is Instance variables. It is possible to have the method with same name as the class.Constructor is not inherited.

Fun practice – Euler problems 

smallest.jpg

Object should not be used unless it is correctly initialized.
One way to do is by placing the initialization code in the constructor.

public class duck

int size;
public duck(int ducksize)
{
System.out.println(“quack”);
size = ducksize;
System.out.println(“size is”+size);
}
}
public class useaduck
{
public static void main(String[] args)
{
duck d = new duck(42);
}
}

Let say in the example of a duck we need to set the size, what if the person who invoked it does not know the value in which case there has to be a default value.

To fulfill the above criteria we can have a default constructor and parameterized constructor.

public class duck
{
int size;

public duck()
{
size = 20;
}
public duck(int ducksize)
{
size = ducksize;
}
}

if you write a parameterized constructor then the compiler wont provide default constructor.If you were to create any constructor, compiler will back off from its duty.

overloading constructors. Constructors can be overloaded , provided they all have different argument list. Constructors can be public, private or default. 

Side note – When an object is created that object will have all the instance variables associated with it and up-to the inheritance curve. That means Object will have a copy of variables from Parent class etc.

All the constructors in an objects inheritance tree must run when you make a new object. Technically even Abstract class will have its constructor run. For an object to be fully formed super class constructor must run to build the super class parts of the object. All the instance variables from every class in the inheritance tree have to be declared and initialized. Even if Super class has instance variables that are private, because the methods will be inherited by the sub class.

object.jpg

When a constructor runs, it immediately calls its superclass constructor, all the way lip the chain until you get to the class Object constructor.

The Order in which a constructor gets called is Parent and then the child.

objects3
The Parent class constructor can be called using the keyword super.

public class duck extends animal
{
int size;

public duck(int newsize)
{
super();
size = newsize;
}
}

key point to note is, even though you would not call the constructor of the parent class the compiler will still call, But if the compiler is gonna call, it will call only the default constructor ( Even if the parent class has a overloaded constructor the default constructor is the only one that will be called)

call to the super class constructor must be the first statement.
Below will not compile

Public cat(int csize)
{
size = csize;
super();
}

public abstract class animal
{
private string name;

public string getname()
{
return name;
}

public animal(string thename)
{
name = thename;
}
}
public class hippo extends animal
{
public hippo(string name)
{
super(name);
}
}
public class makehippo
{
public static void main(String[] args)
{
hippo h = new hippo(“Buffy”);
System.out.println(h.getname());
}
}

Above example, Hippo has to call the super class constructor with name because Hippo does not have the name instance variable. Hippo depends on Animal to return the name through getname(). 

this() refers to the current object and it can be used only within a constructor. and it must be the first statement.every constructor can have either super or this but not both.
I dont get it why use this over super.

Local variables , the ones in the method will be alive only within the method.
Instance variables are associated with Object. So as long as the object is in scope the variables will be alive.

An Objects life depends on the life of references to it.

Life
A local variable is alive as long as its Stack frame is on the Stack. In other words,
until the method. completes.

Scope
A local variable is in scope only within the method in which the variable was declared.When its own method calls another, the variable is alive, but not in scope until its method resumes. You can use a variable only when it is in scope.

reference variables can be used only when they are in scope. Object is alive as long as there are live references to it.

An object becomes eligible for GC when its last live reference disappears.

Three ways to get rid of an object’s reference:

1) The reference goes out of scope, permanently

void go ()
{
Life z = new Life ()
}

z dies at the end of the method

2) Reference is assigned to another object

Life z = new life();
z = new life();

3) The reference is explicitly set to null

life z = new life();
z = null;

If you use the dot operator on a null reference. you’ll get a Null Pointer Exception at runtime.


When you are looking into the object creation and references, there could be multiple references to the object so as long as all the references are de referenced the object will continue to exist.

let say

a = new class();
b = a;
a = null;

Here even though the reference a is being de referenced the variable b is still pointing to the old object, so the object will not be flagged to garbage collection.


next page 293

More in next part.

References:

Head First Java 2nd Edition

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s