Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 29th, 2006, 06:49 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default package and import problem

Hello,
I have a problem when I package a file and import the file in another file.
This is my program:

package zyntz;
// the classes in this file are part of this package


import java.util.*;
// import statements come after the package statement

public class Employee
{
    public Employee(String n,int a , int b, int c, int d)
    {
        name = n;
        salary = a;

    }

    //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;
    }

    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);//·µ»Ø0~9999µÄËæ»úÊý
    }

    public void raiseSalary()
    {
    }

    private String name = ""; //instance variable initialization
    private double salary;
    private int id;
    private static int nextId;
//################################################## ###
import zyntz.*;
// the Employee class is defined in that package


public class PackageTest
{
    public static void main(String[] args)
    {
        //because of the import statement, we don't have to
        //use com.zyntz.java.Employee here
        Employee harry = new Employee ();

        //raise salary by 5%
        harry.raiseSalary();
        //print out information about harry
        System.out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());

    }

}
But when change the word "*" to "Employee",the program is right!
Please tell me why.
Thank you very much!


Prince





Similar Threads
Thread Thread Starter Forum Replies Last Post
Package problem dglnz JSP Basics 9 April 11th, 2005 03:35 AM
Import Data from a form to a word package aysha Access 0 April 4th, 2005 09:23 AM
CH7 - import package kizmit JSP Basics 4 June 16th, 2004 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.