Download Java Programming: Access, Final Fields, Static Methods, Object Construction, Packages and more Slides Java Programming in PDF only on Docsity!
Programming
Access Privileges
- Public
- Private
- A method can access the private data of all objects of its class.
- Example
class Box{
private int L,W,H;
public void test(Box b){
b.H=10;
}
}
Static Fields and Methods
- Keyword : static
- Static Fields
- Only one copy of such instance field is created
- Objects share one copy of static instance fields
- (Class field)
- Static Constants
- Same as static fields except that the value is not modifiable
- Static Methods
- Cannot be called by objects
- Cannot use instance fields
- Called by the class
- Can access only static fields
- (Class method)
Method Parameters
- Call by value
- Call by reference
- Call by address
Object Construction and Initialization
public class Flower { private int petalCount = 0; private String s = new String("null"); Flower(int petals) { petalCount = petals; System.out.println( "Constructor w/ int arg only, petalCount= "
- petalCount); } Flower(String ss) { System.out.println( "Constructor w/ String arg only, s=" + ss); s = ss; } Flower(String s, int petals) { this(petals); //! this(s); // Can't call two! this.s = s; // Another use of "this" System.out.println("String & int args"); }
Flower() { this("hi", 47); System.out.println( "default constructor (no args)"); } void print() { //! this(11); // Not inside non-constructor! System.out.println( "petalCount = " + petalCount + " s = "+ s); } public static void main(String[] args) { Flower x = new Flower(); x.print(); } } ///:~ (source : Internet)
Initialization
- Explicit Field Initialization
- Initialization at the time of declaration
- Constructors
- Initialization within constructors
- Initialization Blocks
- A block of code placed any where in the class, not a very common practice Example
public class Box{ int x,y,w; { x=y=x=5; } }
Initialization block for static fields
public class Box{ int x,y,z;
static int boxCount;
static { boxCount=0; x=y=z=5;
} }
Order of initialization
- When a constructor is called
- All data fields are initialized to their default values
- All field initializers and initialization blocks are executed
- If the first line of the constructor calls a second constructor then the body of the second constructor is executed
- The body of the constructor is executed
Example -
class Employee 26.{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
}
public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
}
// the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
Core Java™ 2: Volume I–Fundamentals 176
- public int getId()
- {
- return id;
- }
- // object initialization block
- {
- id = nextId;
- nextId++;
- }
- // static initialization block
- static
- {
- Random generator = new Random();
- // set nextId to a random number between 0 and 9999
- nextId = generator.nextInt(10000);
- }
- private String name = ""; // instance variable initialization
- private double salary;
- private int id;
- private static int nextId; 81.}
Object Destruction
- C++ uses destructors for objects that are not more in use
- Java provides automatic garbage collection so no destructor is
required
- Garbage collection is implemented through JVM, all objects having no reference are reclaimed by GC
- GC is a daemon thread which cannot be invoked by user program
- Before GC reclaim memory from orphan objects it execute finalize method if there is any
- Memory management in java
- GC takes away most of the work and lets programmer focus on other issues
- If there are resources used by object such as files or sockets or ports programmer should not rely on finalize method and such resources should be freed (by any method or code ) as soon as they are spared by the object
Packages
- Lets you organize your work
- Provides uniqueness to class name
- Standard java library is distributed over a number of packages
( java and javax packages)
- SUN’s recommendations
- Use company’s internet domain name in reverse
- e.g. com.sun.corejava
Class Importation
- In import statement * can be used to import a single package
- Correct Imports
- In Correct Imports
- import java.*;
- import java..;
- Special Imports
- import static java.lang.System.*;
- Import static .java.lang.System.out;
- Provides ease of use in some cases , can also create confusions Example System.out.println() vs out.println(“”); or println(“”); Math.sqrt() vs sqrt()
Package Scope
- Default features can be accessed by all classes in the package
- If we create a class with package java.awt would it become part of standard java.awt package?
- If it becomes part of this package it can use all default features of all the classes in the package
Example (Java standard library class) public class Window extends Container{ String warningString; }
- Restrictions Imposed
- No user defined class starting with java*
- Later we will discuss how a user can protect addition of classes to its package