Java Programming: Access, Final Fields, Static Methods, Object Construction, Packages, Slides of Java Programming

An in-depth exploration of various java programming concepts including access privileges, final instance fields, static fields and methods, method parameters, object construction, initialization, parameter names, order of initialization, object destruction, packages, class importation, package scope, and documentation comments.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

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.{

  1. // three overloaded constructors

  2. public Employee(String n, double s)

  3. {

  4. name = n;

  5. salary = s;

  6. }

  7. public Employee(double s)

  8. {

  9. // calls the Employee(String, double) constructor

  10. this("Employee #" + nextId, s);

  11. }

  12. // the default constructor

  13. public Employee()

  14. {

  15. // name initialized to ""--see below

  16. // salary not explicitly set--initialized to 0

  17. // id initialized in initialization block

  18. }

  19. public String getName()

  20. {

  21. return name;

  22. }

  23. public double getSalary()

  24. {

  25. return salary;

  26. }

Core Java™ 2: Volume I–Fundamentals 176

  1. public int getId()
  2. {
  3. return id;
  4. }
  5. // object initialization block
  6. {
  7. id = nextId;
  8. nextId++;
  9. }
  10. // static initialization block
  11. static
  12. {
  13. Random generator = new Random();
  14. // set nextId to a random number between 0 and 9999
  15. nextId = generator.nextInt(10000);
  16. }
  17. private String name = ""; // instance variable initialization
  18. private double salary;
  19. private int id;
  20. 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
      • import java.util.*;
    • 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