The Artima Developer Community
Sponsored Link

Java Answers Forum
a doubt in chapter 5 of Bruce Eckel's Textbook

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Praveen Namburi

Posts: 1
Nickname: nln
Registered: Jun, 2004

a doubt in chapter 5 of Bruce Eckel's Textbook Posted: Jun 20, 2004 8:09 PM
Reply to this message Reply
Advertisement
Hi!


I was successfully able to excecute c05:LibTest.java (the java program in chapter 5) given below.


 
//: c05:LibTest.java
// Uses the library.
import com.bruceeckel.simpletest.*;
import com.bruceeckel.simple.*;
 
public class LibTest {
  static Test monitor = new Test();
  public static void main(String[] args) {
    Vector v = new Vector();
    List l = new List();
    monitor.expect(new String[] {
      "com.bruceeckel.simple.Vector",
      "com.bruceeckel.simple.List"
    });
  }
} ///:~
 



The Vector.java and List.java are present in the com/bruceeckel/simple directory and are given below


//: com:bruceeckel:simple:Vector.java
// Creating a package.
package com.bruceeckel.simple;
 
public class Vector {
  public Vector() {
    System.out.println("com.bruceeckel.simple.Vector");
  }
} ///:~
 
//: com:bruceeckel:simple:List.java
// Creating a package.
package com.bruceeckel.simple;
 
public class List {
  public List() {
    System.out.println("com.bruceeckel.simple.List");
  }
} ///:~



Now in Vector.java I have removed the public access specifier for the Vector class and have made the constructor private as below.


 
//: com:bruceeckel:simple:Vector.java
// Creating a package.
package com.bruceeckel.simple;
 
class Vector {
 private Vector() {
    System.out.println(
      "com.bruceeckel.simple.Vector");
  }
} ///:~



I have compiled the LibTest.java and thought it shouldnt work because the constructor was declared private but it compiled and executed safely. Why is it so? If that is the case then even this program given below shouldnt complain


//: c05:IceCream.java
// Demonstrates "private" keyword.
 
class Sundae {
  private Sundae() {}
  static Sundae makeASundae() {
    return new Sundae();
  }
}
 
public class IceCream {
  public static void main(String[] args) {
    Sundae x = new Sundae();
    //Sundae x = Sundae.makeASundae();
  }
} ///:~
 



Can anyone explain me the difference in behavior between the LibTest.java and and the Icecream.java programs?

Topic: problem creating login system Previous Topic   Next Topic Topic: javax.ejb package

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use