Showing posts with label Java Basics. Show all posts
Showing posts with label Java Basics. Show all posts

Monday, July 5, 2010

Constructors

A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. The return type of a class’ constructor is the class type itself. It is called immediately after the object is created, before the new operator completes.

The “this” Keyword

“this” keyword is used by a method when it needs to refer to the object that invoked it. this can be used inside any method to refer to the current object.

Class Variable, Instance Variable, Local Variable

Class Variable
Static variables declared inside a class are referred to as class variables. They are global to all objects created by the class.

Instance Variable
Instance variables are variables defined in a class body which are created when objects are instantiated. They take different values for each object.

Local Variable
Variables declared and used inside methods are called local variables.

Instance Variable Hiding

When a local variable has the same name as an instance variable, the local variable hides the instance variable.

Garbage Collection

Java provides auto garbage collection for unused objects. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects.

The finalize( ) Method

By using the finalize method, we can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector. The Java run time calls this method whenever it is about to recycle an object of that class. Inside the finalize( ) method you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects.

Overloading Methods

Method overloading is one of the ways that Java implements polymorphism. When two or more methods within the same class share the same name but differ in the type and/or number of their parameters then those methods are said to be overloaded. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call.

Argument Passing

Call-by-value: This method copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument.


Call-by-reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine.

When a simple type is passed to a method, it is done by use of call-by-value. Objects are passed by use of call-by-reference.

Access Specifiers

How a member can be accessed is determined by the access specifier that modifies its declaration. Java’s access specifiers are public, private, and protected. Java also defines a default access level. protected applies only when inheritance is involved.

• When a member of a class is modified by the public specifier, then that member can be accessed by any other code.

• When a member of a class is specified as private, then that member can only be accessed by other members of its class.

• When a member of a class is specified as protected, then that member of a class is public within its own package and in subclasses in other packages, but cannot be accessed by non-subclasses outside its package.
• When no access specifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package.

Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member does not have an explicit access specification the default access is used, it is visible to subclasses as well as to other classes in the same package. If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected.


PrivateDefaultProtectedPublic
Same classYesYesYesYes
Same package subclassNoYesYesYes
Same package non-subclassNoYesYesYes
Different package subclassNoNoYesYes
Different package non-subclassNoNoNoYes

The “static” Keyword

When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ).


Variables

Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods

Methods declared as static have several restrictions:
• They can only call other static methods.
• They must only access static data. They can not to refer to any instance variables.
• They cannot refer to this or super in any way.

If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded.

Arrays Revisited

Arrays are implemented as objects.


int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};

Java Command-Line Arguments

All command-line arguments are passed as strings.

javac prog.java
java prog this is test no 1

args[0]: this /* Arguments can be accessed by using args[i] */
args[1]: is
args[2]: test
args[3]: no
args[4]: 1 /* You must convert numeric values to their internal forms manually: Integer.parseInt(str) */