Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Lightbulb Using one class object in other package's class

    Hello,

    I am using netBeans 7.0 for developing java application. I have multiple packages containing multiple classes. The question is that i want to create on class such that when i instantiate that class , i want to use that object into other packages's classes without re-instantiating the same one because i have stored some values inside that class.

    How can i do this?

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Using one class object in other package's class

    Pass a reference to the object to the methods where you want to access its contents.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Using one class object in other package's class

    I think you may be hung up on how to make the reference to the object. Just an on the fly example, not the best, but to illustrate.

    Let's say you have a class called Customer, that has name and age attributes. Then, you have a 2nd class called Claim(as in insurance claim) that has a Customer attribute, and a method called process() that processes customer data, along with any other information relative to the claim. The reference to a customer from within the Claim class may look like:
    Java Code:
    package mypackage;
    import com.Customer;
    
    public class Claim {
        private Customer customer;
        private int number;
        
        public Claim(Customer customer, int number){
            this.customer = customer;
            this.number = number;
        }
        
        public Customer getCustomer(){
            return customer;
        }
        
        public int getNumber(){
            return number;
        }
        
        public void processClaim(){
            //do something with Customer
        }
        
    }
    Java Code:
    package com;
    
    public class Customer {
        private String name;
        private int age;
        
        
        public Customer(String name, int age){
            this.name = name;
            this.age = age;
        }
        
        public String getName(){
            return name;
        }
        
        public int getAge(){
            return age;
        }
        
        public void setName(String name){
            this.name = name;
        }
        
        public void setAge(int age){
            this.age = age;
        }
        
    }

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 05:37 AM
  2. Replies: 0
    Last Post: 03-27-2011, 06:49 AM
  3. Replies: 9
    Last Post: 12-15-2010, 03:38 AM
  4. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 01:30 AM
  5. Compiling a class in a package from cmd
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 11:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •