Results 1 to 6 of 6
  1. #1
    macylapka is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Why don't create new object instance ?

    Hi,

    Pls forgive me my poor english.

    Java Code:
    public static void main(String[] args) 
    {
    Pracownik henryk = new Pracownik("Henryk Paker", 35000, 1996, 12, 1);
    System.out.println("henryk = " + henryk);
    		
    Pracownik koder = new Pracownik("Henryk Paker", 35000, 1996, 12, 1);
    System.out.println("Przed podwyzka henryk.equals(koder) zwraca " + henryk.equals(koder));
    }
    I need to 2 instance of Pracownik class but this code make only one, henryk, and koder is reference to henryk. How solved this ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why don't create new object instance ?

    Your code actually makes two Pracownik instances, but they are functionally equal to each other as they satisfy the requirements of the Pracownik's equals method. To see that this is so, test for reference equality with ==.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why don't create new object instance ?

    But you have two instances of the Pracownik class, one is henryk and the other one is koder. Their content is probably equal so (if you implemented the equals( ... ) method correctly) they turn out to be equal.

    kind regards,
    os
    J
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    macylapka is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Why don't create new object instance ?

    my equals method :

    Java Code:
    public static boolean equals(Object a, Object b)
    	{
    		if(a == b)
    			return true;
    		
    		if(a == null || b == null)
    			return false;
    	
    		Class kl = a.getClass();
    		
    		if(kl != b.getClass())
    			return false;
    		
    		do
    		{
    			Field[] pola = kl.getDeclaredFields();
    			AccessibleObject.setAccessible(pola, true);
    			for(int i = 0; i < pola.length; ++i)
    			{
    				Field p = pola[i];
    				try
    				{
    					if(!p.get(a).equals(p.get(b)))
    						return false;
    				}
    				catch(Exception e)
    				{
    					e.printStackTrace();
    				}
    				kl = kl.getSuperclass(); // herer is generated exception java.lang.NullPointerException
    			}
    		}
    		while(kl != Object.class);
    		
    		return true;
    	}
    Last edited by macylapka; 09-29-2011 at 01:48 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why don't create new object instance ?

    Quote Originally Posted by macylapka View Post
    my equals method :
    Great. This doesn't change Jos and my answers.

  6. #6
    macylapka is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Why don't create new object instance ?

    You have right. I make mistake i my equals method. Thx ! :)

Similar Threads

  1. instance of an object
    By RichersooN in forum New To Java
    Replies: 3
    Last Post: 09-21-2011, 10:10 AM
  2. How to create a instance for logger?
    By nirasiva in forum New To Java
    Replies: 3
    Last Post: 06-04-2011, 08:43 AM
  3. [HELP] cannot create instance of jdialog???
    By clydedoris in forum New To Java
    Replies: 0
    Last Post: 07-21-2010, 09:02 AM
  4. object and instance
    By anurag in forum New To Java
    Replies: 1
    Last Post: 05-14-2010, 08:51 PM
  5. Create different instance of a tablemodel
    By Bill in forum AWT / Swing
    Replies: 6
    Last Post: 03-27-2008, 03:49 PM

Posting Permissions

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