Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A little bit a clarification with returning objects.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
Aziz




PostPosted: Sun Jul 30, 2006 1:51 pm   Post subject: A little bit a clarification with returning objects.

In the following code:

Java:

private Element type;

// . . .

public Element getElement()
{
  return type;
}


and later:

Java:
private Element typeOfElement;

// . . .

typeOfElement = getElement();


In my understanding, the 'getElement()' returns a pointer to the 'type' member, right? So, if something like changeType() is called on 'typeOfElement', does this change 'type' in the first code snippet? 'typeOfElement' and type point to the object, right? I want to make 'typeOfElement' a new object, but with all the same information as 'type'. Would I use the copy() method, or what?
 
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Sun Jul 30, 2006 1:56 pm   Post subject: (No subject)

there are no pointers in java. all objects are pass by reference. to make another copy, either implement the copy constructor or override the clone method
 
Aziz




PostPosted: Sun Jul 30, 2006 2:16 pm   Post subject: (No subject)

Okay, thought so, thank you for the clarification Smile
 
wtd




PostPosted: Sun Jul 30, 2006 2:57 pm   Post subject: (No subject)

Consider the following. I use Scala because it has an interactive interpreter and syntax I find nicer to work with than Java. The semantics remain relevant, though.

code:
scala> class A(private val b: java.util.ArrayList) { def getB = b }
defined class A

scala> val a = new A(new java.util.ArrayList)
a: line15$object.A = line15$object$A@1ad0839

scala> val b = a getB
b: java.util.ArrayList = []

scala> b add 42
line18: scala.Boolean = true

scala> b
line19: java.util.ArrayList = [42]

scala> a getB
line20: java.util.ArrayList = [42]

scala>
 
Aziz




PostPosted: Sun Jul 30, 2006 3:49 pm   Post subject: (No subject)

Yes, I see, and I actually understood that.

Anyways, another question.

Java:
public Element getType()
    {
        return type;
    }
   
    public void setType(Element type)
    {
        this.type = type;
    }
   
    public static void main(String[] args)
    {
        Weapon sword;
        ElementalWeapon fireSword;
       
        sword = new Weapon("Sword", "A sword", 30, 10);
        fireSword = new ElementalWeapon("Fire Sword",
                "A flaming hot sword", 70, 10, new FireElement());
       
        if (fireSword instanceof Elemental &&
            fireSword.getType() instanceof FireElement)
        {
            System.out.println("This sword is elemental" +
                               "and of the fire element");
        }       
       
    }


I want to be able to compare if the object is elemental, and if it is a ceratin type (ie FireElement) but I'm not sure if 'return type;' is safe, as anything else could change it. How would I implement a copy() or clone() method in Element, I've tried and no succes. The CloneNotSupportedException is throwing me off, even when I implement clone.
 
wtd




PostPosted: Sun Jul 30, 2006 5:26 pm   Post subject: (No subject)

Can I see your other classes?
 
wtd




PostPosted: Sun Jul 30, 2006 8:13 pm   Post subject: (No subject)

bugzpodder wrote:
there are no pointers in java. all objects are pass by reference.


Actually, it's worth noting that you have this exactly backwards.

In Java all Object type variables are handled by pointers. You just can't arbitrarily manipulate these pointers.

These pointers are then passed by value to methods.

Were they pass by reference, we could do something like:

code:
public class Test {
   private static String testString = "Hello";

   private static void test(String s) {
      s = "world";
   }

   public static void main(String[] args) {
      System.out.println(testString);
      test(testString);
      System.out.println(testString);
   }
}
 
Aziz




PostPosted: Sun Jul 30, 2006 10:07 pm   Post subject: (No subject)

I've included the appropriate source files. Thanks wtd.

code:
package rpg.element;

public class Element
{   
    private String name;
   
    public Element(String name)
    {
        this.name = name;
    }
   
    public String getName()
    {
        return name;
    }
   
    public String toString()
    {
        return name;
    }
}



code:
package rpg.item.weapon;

import rpg.element.*;

public class ElementalWeapon extends Weapon implements Elemental
{
    private Element type;

    public ElementalWeapon(String name, String desc, int value, int damage, Element type)
    {
        super(name, desc, value, damage);
       
        this.type = type;
    }
   
    public Element getType()
    {
        return type;
    }

    public void setType(Element type)
    {
        this.type = type;
    }

public static void main(String[] args)
    {
        Weapon sword;
        ElementalWeapon fireSword;
       
        sword = new Weapon("Sword", "A sword", 30, 10);
        fireSword = new ElementalWeapon("Fire Sword",
                "A flaming hot sword", 70, 10, new FireElement());
       
        if (fireSword instanceof Elemental &&
            fireSword.getType() instanceof FireElement)
        {
            System.out.println("This sword is elemental" +
                               "and of the fire element");
        }       
       
    }
}


code:
package rpg.element;

import rpg.element.Element;

public class FireElement extends Element
{
    public FireElement()
    {
        super("Fire");
    }
}
 
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Jul 30, 2006 11:01 pm   Post subject: (No subject)

Well, one perhaps less than immediately relevant suggestion:

The methods in your Elkemental interface should probably be getElementType and setElementType.

I'll look more closely tomorrow.
 
bugzpodder




PostPosted: Sun Jul 30, 2006 11:13 pm   Post subject: (No subject)

yes you are right wtd. in fact i think i've posted a site a while ago that explains this clearly than most others. it made two points which kind of amusing. cant find it though.
 
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: