Saturday, November 30, 2013

Java Structure

Java program is made of
1. Objects
2. References.

Objects are stored in the heap.
References are stored on stack.

Objects derive all other objects in java.

E.g.
Class is an object.
Class essentially comprise of:
1)fields(data members)
2)Methods(member functions)

All class variables are initialized to default values. But if there is a variable defined inside a method. Then it does not get initialized to a default value.

Methods transfer messages among objects and are created only inside classes.

Method comprise of:
1. Name
2. Its arguments.
3. Its return type.

Static in Java:
When we use static keyword it means that that particular method or field is not associated to any class instance(i.e. object of that class).

e.g.

class Foo {
static int a = 10;
}

Now I create two instances of the class Foo.
Foo object_1 = new Foo();
Foo object_2 = new Foo();

System.out.println(object_1.a);
System.out.println(object_2.a);
System.out.println(Foo.a); //As they are not associated to any instance, they can be used directly with their className
Will give the same answer that is 10

The memory location for the static a will be the same across all instances.

Naming conventions in Java:
The class name starts with a capital with running words (if it contains more words), first letter of which is capital. e.g.

class ThisIsAnExampleClassName{}

No comments:

Post a Comment