Two equal objects, but different classes?

When programming on binding Referenceable object with JDK version 1.5.0_06, I have encountered a very strange phenomenon: two objects are equal, but they belong to different classes!!!
The source codes of the program bind_ref.java are listed as below:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;
import java.util.Hashtable;
public class bind_ref {
public static void main( String[] args ) {
// Set up environment for creating the initial context
Hashtable env = new Hashtable();
env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory" );
env.put( Context.PROVIDER_URL, "file:/daniel/" );
Context ctx = null;
File f = null;
Fruit fruit1 = null, fruit2 = null;
byte [] b = new byte[10];
try {
ctx = new InitialContext( env );
Hashtable the_env = ctx.getEnvironment();
Object [] keys = the_env.keySet().toArray();
int key_sz = keys.length;
fruit1 = new Fruit( "Orange" );
     SubReference ref1 = fruit1.getReference();
ctx.rebind( "reference", fruit1 );
     fruit2 = ( Fruit )ctx.lookup( "reference" );
     System.out.println( "ref1's class = (" + ref1.getClass().toString() + ")" );
     System.out.println( "fruit2.myRef's class = (" + fruit2.myRef.getClass().toString() + ")" );
     System.out.println( "( ref1 instanceof SubReference ) = " + ( ref1 instanceof SubReference ) );
     System.out.println( "( fruit2.myRef instanceof SubReference ) = " + ( fruit2.myRef instanceof SubReference ) );
     System.out.println( "ref1.hashCode = " + ref1.hashCode() + ", fruit2.myRef.hashCode = " + fruit2.myRef.hashCode() );
     System.out.println( "ref1.equals( fruit2.myRef ) = " + ref1.equals( fruit2.myRef ) );
} catch( Exception ne ) {
System.err.println( "Exception: " + ne.toString() );
System.exit( -1 );
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
All the outputs are shown as below:
=======================================================
Fruit: I am created at Mon Jun 18 11:35:13 GMT+08:00 2007
SubReference: I am created at Mon Jun 18 11:35:13 GMT+08:00 2007
--------- (i)subref.hashCode() = (-1759114666)
SubReference: I am created at Mon Jun 18 11:35:13 GMT+08:00 2007
--------- (i)subref.hashCode() = (-1759114666)
FruitFactory: obj's class = (class javax.naming.Reference)
FruitFactory: obj's hashCode = -1759114666
FruitFactory: obj = (Reference Class Name: Fruit
Type: fruit
Content: Orange
FruitFactory: ( obj instanceof SubReference ) = false
FruitFactory: subref_class_name = (Fruit)
Fruit: I am created at Mon Jun 18 11:35:13 GMT+08:00 2007
ref1's class = (class SubReference)
fruit2.myRef's class = (class javax.naming.Reference)
( ref1 instanceof SubReference ) = true
( fruit2.myRef instanceof SubReference ) = false
ref1.hashCode = -1759114666, fruit2.myRef.hashCode = -1759114666
ref1.equals( fruit2.myRef ) = true
========================================================
I hightlight the critical codes and outputs related to the strangeness with bold texts.
Who can tell me what happens? Is it really possible that two objects belonging to different classes are equal? If so, why that?

It can also depend on how you implement the equals method.
class Cat {
    String name;
    Cat(String n) {
        name = n;
class Dog {
    String name;
    Dog(String n) {
        name = n;
    public boolean equals(Object o) {
        return name.equals(o.name);
    public static void main(String[] args) {
        Dog d = new Dog("Fred");
        Cat c = new Cat("Fred");
        System.out.println(d.equals(c));
}

Similar Messages

  • When it's required to share a single object in different classes????

    Hi friends...
    I am new to java programming language....
    when it is required to share a single object in different classes???
    and
    please give me one example with explanation...
    Thank you
    regards Shree

    sun_shree wrote:
    Thanks for all giving reply.....
    please write the constructor which is accepting reference and please,,,,,, explain......
    Thanking youNo.
    This will be covered in any Java textbook or tutorial. Like this one: [http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html]
    After reading it and writing some code of your own to test your understanding, if you still have a specific question, post again.

  • Same setting for two file adapter but different output

    Hi ,
    I am having two receiver file adapter for two differrent scenario with same settings but different structure.
    I want the output of the file for both the scenario should be displayed line by line but it is working fine for one scenario.File type,file encoding and FCC are same in both the interface except the fieldsnames in FCC.
    When i check both the output file i found that the scenario which is working fine  having OA OD hexadecimal value at the end of line of each line.and the second scenario which is giving the output in single line is having only OA.
    waitng for Valueable inputs!!!
    Thanks
    Santosh

    Hi Santosh,
    Kindly elaborate your question.
    it would be helpful if you post the content conversion parameters of the erroneous file channel.
    Regards,
    Varun

  • How to access form objects from different class?

    Hello, I am new to java and i started with netbeans 6 beta,
    when i create java form application from template i get 2 classes one ends with APP and one with VIEW,
    i put for example jTextField1 with the form designer to the form and i can manipulate it's contents easily from within it's class (let's say it is MyAppView).
    Question>
    How can i access jTextField1 value from different class that i created in the same project?
    please help. and sorry for such newbie question.
    Thanks Mike

    hmm now it says
    non static variable jTree1 can not be referenced from static context
    My code in ClasWithFormObjects is
    public static void setTreeModel (DefaultMutableTreeNode treemodel){
    jTree1.setModel(new DefaultTreeModel(treemodel));
    and in Class2 it is
    ClasWithFormObjects.setTreeModel(model);

  • PFCG, two roles with the same object but different values

    Hi, Can you help me?
    I need to know if it's possible have two roles like this:
    role A - Object werks = L001 and LIKP-LFART = LF
    role B - Object werks = L005 and LIKP-LFART = ZLF
    If the some user have role A and role B it's possible that he doesn't have authorization for werks = L005 and LIKP-LFART LF?
    Thanks
    Dora

    I guess you made fat figure on the words: "it's possible that he doesn't have authorization for werks = L005 and LIKP-LFART ZLF", right?
    If so, it is impossible.
    When SAP doing the authorization check, it call the function "authority_check", input the Object, the filed and the value to check.
    if some one have role A and B, SAP will check authority both in Role A and Role B.
    What you need to do should be separating the Object into a subrole and assign it separately.
    >
    Jorge Sousa wrote:
    > Hi, Can you help me?
    > I need to know if it's possible have two roles like this:
    > role A - Object werks = L001 and LIKP-LFART = LF
    > role B - Object werks = L005 and LIKP-LFART = ZLF
    > If the some user have role A and role B it's possible that he doesn't have authorization for werks = L005 and LIKP-LFART LF?
    >
    > Thanks
    >
    > Dora

  • Receiving Objects of different classes??

    Hi,
    i have a little question...I know how to receive an ObjectMessage in JMS and how to retrieve the object from the message, but so far the message receiver knew what Object it is about to receive..and hence I used a cast like this:
    Person p = (Person) objectmessage.getObject();
    Now, I want, that the receiver does not know in advance what object it is about to receive....Does anybody have a good Idea??...Can I use something like this??
    if (objectmessage.getClass().getName.equals("de.bla.Person"))
    Person p = (Person) objectmessage.getObject();
    Is this a good idea??...Ok. thanks...

    it's probably better if you use an overloaded type and send this as the payload of your ObjectMessage.
    e.g., inheritance relationships:
    A <- B
    A <- C <- D
    Then you send and receive Objects of type A. There could be an overloaded method m (with different implementations according to which actual class the received object has). Like this you don't need explicit casts or class checks and let the JVM do the runtime type checking more efficiently and more object-oriented.

  • How to make versionable object of different class than Document?

    In the programmers reference is that every PublicObject can be versioned, but there are only samples for versioning documents. I would like to keep history on folder hierarchy changes, so I think I need a Folder that can be versioned.
    The question is how to do this?
    Should I override isVersionable method in subclass of Folder to return true?
    Please help

    Hi
    I am havin 10 text boxes in my application.
    But out of 10 , 3 will be always together(existence of one is not possible without the other two.)
    Now i want to create a vector of objects ( here object will consist of the value corresponding to these three boxes),there can be many elements in this vector.
    So i m thinking to make an inner class which have three String variables corresponding to these text boxes that exists together.
    What can b done ?

  • What's the best way to organize a sequence of objects of different classes

    Say I have a base class and some derived classes... lots of different derived classes, actually. I need to create many objects from all sorts of these subclasses and put them onto a LIFO sequence. What's the ideal type of collection to handle this? I'd also like to shuffle and pop elements, as well. I know about Vectors and Stacks, but I am wondering... Is it possible to have different types of these objects floating around in them? Would I have to use generics?
    I know you can create a list of the superclass and simply cast it to that, but I read that was bad practice.

    mhoribe wrote:
    I see.
    Let me ask you this. What, exactly, goes on when casting? If I add new methods and field to a subclass and cast it back to the base class, will I get any trouble using methods and fields? Or does Java simply allow this for simplicity sake. Like, we know it's a base class, but I'll allow you to call it a superclass just so it will be compatible and simple. Kinda get what I'm going for here?You never need to cast something to a super. Every instance of a subclass IS and instance of all its superclasses and interfaces.
    However, if you're adding new methods and fields to a subclass, there's a good chance you're subclassing for the wrong reasons, and especially if you're storing objects of these various classes in the same data structure but not treating them as a common parent type, you probably have a broken design.

  • Created serializable object, but get "Class not found" on "createObjectMessage()" cal

    I'm using OC4J 9.0.2.1 and JDeveloper 9.0.2 with Oracle db. I have a set of queues created in the database.
    Before I try to send my request, I create an instance of the serializable object I want to send. I do all of my queue connection setup, then I call:
    queueSession.createObjectMessage(request);
    Where "queueSession" is a QueueSession obtained from "queueConnection.createQueueSession()".
    When I step over this function call, I get the following:
    oracle.jms.AQjmsException: JMS-109: Class not found: com.attws.it.bsa.felix.mci.common.ServiceInfoRequest
    (The indicated class name is the type of the "request" object.)
    What could I be doing wrong?

    The code below creates the following classes when I compile it (using jsk1.3):
    DistanceBarApplication2.class
    DistanceBarApplication2$1
    DistanceBarApplication2$2
    DistanceBarApplication2$3
    DistanceBarApplication2$DistanceBar2
    The error I receive is: load: class DistanceBarApplication2 not found
    Here is my HTML code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD>
    <BODY>
    <applet code="DistanceBarApplication2.class" width=300 height=50>
    </applet>
    </BODY>
    </HTML>
    Here is the Java code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class DistanceBarApplication2 extends JApplet {
    private final DistanceBar2 bar = new DistanceBar2();
    public void init() {
    bar.setMaximum(100);
    bar.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    bar.stop();
    JOptionPane.showMessageDialog(bar, bar.getValue() + " yards");
    bar.start();
    this.getContentPane().add(bar);
    bar.start();
    static class DistanceBar2 extends JProgressBar {
    private Thread mThread;
    private boolean mRunning= false;
    private int mValue = 0;
    private boolean mUp = true;
    private Runnable updater = new Runnable() {
    public void run() {
    setValue(mValue);
    public void start() {
    if (mRunning) {
    return;
    mRunning = true;
    mThread = new Thread(new Runnable() {
    public void run() {
    while (mRunning) {
    try {
    Thread.sleep(30);
    } catch (InterruptedException e) { }
    adjust();
    mThread.setDaemon(true);
    mThread.start();
    public void stop() {
    mRunning= false;
    // Best to let a thread run itself out
    // if (mThread != null)
    // mThread.interrupt();
    // Don't need this for this applet, but possibly later
    // public Dimension getPreferredSize() {
    // return new Dimension(300,70);
    private void adjust() {
    int delta = getMaximum() / 40;
    mValue += delta * (mUp ? 1 : -1);
    if (mValue >= getMaximum()) {
    mValue = getMaximum();
    mUp = false;
    } else if (mValue <= 0) {
    mValue = 0;
    mUp = true;
    SwingUtilities.invokeLater(updater);

  • Passing variables/objects to different classes

    I need to get the balance of bank accounts for the method "total" .
    Problem is I haven't written the class "Account" and therefore don't get to grips with it...
    Any ideas how I can get it to work?
    Thanks a lot
    class Account
        private double theBalance    = 0.00;   // Balance of account
        private String theName       = "";
        public Account( String name, double openingBalance )
          theBalance = openingBalance;
          theName    = name;
        public double getBalance()
          // assert theBalance >= 0.00;
          return theBalance;
        public String getName()
          return theName;
        public double withdraw( final double money )
          // assert theBalance >= 0.00;
          if ( theBalance >= money  )
            theBalance = theBalance - money;
            return money;
          } else {
            return 0.00;
        public void deposit( final double money )
          // assert theBalance >= 0.00;
          theBalance = theBalance + money;
      class BankStats {
           private Account accounts[];
          public BankStats(Account[] acc) {
               accounts = acc ;
          public double total (double ammount) {
               double bal = Account.getBalance(accounts);
         }

    Problem is I haven't written the class "Account" and therefore don't get to grips with it...Sorry, what's that stuff "class Account"? What do you mean you haven't written it? It's right there!
    Any ideas how I can get it to work?You haven't written Account in a such a way that you'll get the answer you want, but that's different.
    I do have an idea, but I'm not going to write it for you. Have a look at your total method. Does the compiler complain when you try to compile this? Something about static methods? Might want to read about that, and think about what that array of Accounts is really for. Maybe BankStats should loop over the array of Accounts and get their respective balances.
    When is your homework due?
    MOD

  • Two audio interfaces but different uses in one system

    Hi,
    I have already got an audio interface for recording and run smoothly.
    Recently I get another audio interface (M-Audio Firewire 410). I want to add it to my system as a standalone mixer in which the outputs will be connected to 2 another active monitors, the inputs of this "mixer" are connected to audio outputs of a DVD player.
    So, I can control the DVD volume by the knob on the Firewire 410 and by the M-Audio software in my Mac but my original audio set up doesn't change.
    I mean, the DVD audio signal pass to the Firewire 410 then output to the newly added monitors only,my original monitors will not be affected (no DVD sound).
    At the same, if I play mp3 in i-tunes, the music will output to my original monitors only.
    Just like two independent audio set up in one system, is it possible ?
    Thanks for any help in advance.
    Cheers,
    edtayin

    You need to give complete details of the audio, exactly what is it, how many tracks, what's the channel configuration?
    Can you post screen shots of what you're doing and what happens?
    In expanding audio components in the timeline I can see what looks like an extra audio clip married to this one on a different track which has no signal, but I can't delete that additional audio clip.
    Let's see this and the channel configuration in the audio inspector.

  • Sorting a list of different class objects

    Hi All ,
    How to sort a List a.which contains objects of different Classes, b. objects of same class . Is it possible to sort these by implementing Comparable interface .
    Thanks ,
    Rajesh Reddy

    rajeshreddyk wrote:
    Hi All ,
    How to sort a List a.which contains objects of different Classes, b. objects of same class . Is it possible to sort these by implementing Comparable interface .Well, if objects of different classes are kept in the same List and you want to sort them together they at least have that in common. They're Comparable-able. -:) To manifest that the different classes could all implement a Comparableable interface (or maybe Intercomparable would be a better choise of name.)

  • Compare two similar objects

    Do you know about any possible Java bugs with JDK update?
    In my case, my code stopped working.
    Here is what I have. There are two similar objects, but Object 1 has key, and Object 2 (same as Object 1) has no key. I need to compare both, and if they are equal, I need Object 1 to assign key to Object 2.
    It worked before. How I can get around.
    Please help me if you can.
    Respectfully,
    Alex

    Let me change the question:
    "Can we compare two objects one with key and another one without key? How?

  • How can I create 2 catalogues (same design but different languages)

    Hello,
    I have to launch 2 catalogues (32 pages) in Indesign, with an equal layout but different languages. My client will need to start them both and won't wait for a agreement on a Master file. He'll update the layout all along the realisation (color, pictures and graphic design).  To avoid waisting time by correcting twice, would you have a tip to link both composition and enhance automatically the background layout and ajustements over and above the languages ?
    What would be the best method ?
    Thanks a lot for your advices !
    Caro.

    Welcome to the discussions.
    In the Accounts tab of Mail's preferences, you can set the Full Name (I don't know how it will be labelled in French - it is the 3rd field in Account Information). This is the name that will be displayed in the From column of the recipient's mail client.
    But if the mail accounts are for two different users, it would be better to set up a separate login account on the mac for each of them, and then set up the mail preferences for the separate users.
    AK

  • Two remote objects calls on the same php class

    Hi to all,
           I've encountered a strange issue while developing with remote objects.
    I've a mxml component with an init() method inside which is called by a menu.
    When the init() method is called it makes 7 remote object calls which are bound to some components' dataprovider.
    Among this calls I've got 2 remote object which refer to the same remote class. This because I have to call the class twice and the bind the result to two different combobox. Below you find the code:
    <mx:RemoteObject id="myFile" source="myRemoteClass" destination="amfphp"  showBusyCursor="true" makeObjectsBindable="true" fault="traceFault(event)"/>
    <mx:RemoteObject id="myXls"  source="myRemoteClass" destination="amfphp"  showBusyCursor="true" makeObjectsBindable="true" fault="traceFault(event)"/>
    in the init function I make this calls:
    myFile.listDir("dir_1")
    myXls.listDir("dir_2")
    then in the mxml code I bound the result of myFile to combobox1 and the result of myXls on combobox2.
    The problem arise when I call the myXls' listDir method. When I call it I receive the following error:
    code:
    Client.Error.DeliveryInDoubt
    Message:
    Channel disconnected
    Detail:
    Channel disconnected before an acknowledgement was received
    The strange thing is that not only the myXls object returns this error, but also all the other 6 remote object return the same error above.
    I'm not sure, but I guess that the error could be caused by the two remote object which call the same php remote class. If I comment one of the two calls everything works fine.
    Do you have any suggestion about?
    Thanks!!
    Bye
    Luke

    Hi Jan.
    1) We have the 2 VO, each with 3 rows to fill in data. What I mean is that when i just fill in all the fields for the first row of the first VO, and the value of one of these fields is bigger than 50, then after the exception is thrown and the message is displayed, the fields for the first VO are duplicated and shown in the second VO as if the user had inserted them.
    2) We tried yesterday the validateEntity and a Method and Atributte Validator approaches after reading that white paper with the same results.
    The validation is correctly done using any of the those methods.
    I will try to reproduce this issue with the HR schema.
    Thanks in advance once again.

Maybe you are looking for

  • Backupping system failed

    Urgent help needed. Hello, This evening I tried to backup the files in Recovery Q file. In a 4.7GB DVD. I started with a simple click on the red icon at Computer. It was a long wait, but finally i got something that the first part was succesfully suc

  • Install Fatal Error

    I'm encountering a fatal error when I attempt to install Expressions studio. I have the log if that helps. Tried uninstalling and reinstalling several times and keep getting the same fatal error message.

  • Invoice due date

    Hi Gurus , i want Invoice due date for invoice . points sure ! regards, Rahul Deshmukh

  • File compatibility, pls help

    I'm fairly new to I pod and I was wondering if WMA files are compatible with I pods? Please help

  • DirectoryServices at 157% on 10.6.1 on Macbook 2,1

    Since I've installed SL, The fans on my Macbook (Late 2006) have been out of control. They rarely go off, and the problem seems to be with the DirectoryServices. I've tried reinstalling the OS, but that doesn't seem to help. How can I remedy this? Al