Results 1 to 3 of 3
- 03-21-2012, 01:13 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 23
- Rep Power
- 0
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.
- 03-21-2012, 01:37 PM #2
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.
- 03-21-2012, 02:09 PM #3
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
-
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
Problem using object from hibernate class in a bean class
By ShinTec in forum Advanced JavaReplies: 9Last Post: 12-15-2010, 02:38 AM -
substract Parent class object from child class
By nikosv in forum New To JavaReplies: 0Last Post: 12-08-2010, 12:30 AM -
Compiling a class in a package from cmd
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 10:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks