Passing Information from Objerct to Object

Passing information(Strings) from one object to another object

Page 1 of 1

1 Replies - 755 Views - Last Post: 10 October 2009 - 07:46 AM Rate Topic: -----

#1 gm5660  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 12-August 08

Passing Information from Objerct to Object

Posted 09 October 2009 - 10:20 PM

Please help! I need someone to show me how to write code that will enable information(Strings, Integer, Numbers, etc.) from one object to another. I have tried to extend one class to the other and I get the error: infinate stack overflow. What am i doing wrong with this code? I need Pig to display the String "Cow". Here is the code:

This is the code for Animal:

public class Animal 
   {
	   Cow   moo = new Cow();
	   Pig   oink = new Pig();
	   
	   public static void main(String[]  args)
	   {
		   new Animal();
		}
		
		public Animal()
		{
			System.out.println("Pig   " + oink.getName() + "  and   " +
							   "Cow   " + moo.getName());
				  
		}
	}




This is the code for Cow:

public class Cow 

   {
	   String name = "";	  
	   
	   public void moo()
	   {
		   new Cow();
		}
		
		public void name()
		{
			String name = "Cow";
		}
		
		public String getName()
		{
			return name;
		}
		
	}




This is the Pig code:

public class Pig 
   {
	  String name = "";
	  Animal   group = new Animal();
	  
	  public void oink()
	  {
		  new Pig();
		  System.out.println(group.getName());
		  
		}
		
	  public void name()
	  {
		  String name = "Pig";
		}
	  
		public String getName()
		{
			return name;
		}
	   
		
	}




I have even tried extending the Animal class with either Pig or Cow and I still get the error code 'infinate stackcounter overflow'. Can anyone explain what I'm coding wrong and get me on the right track?

This post has been edited by gm5660: 09 October 2009 - 10:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing Information from Objerct to Object

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Passing Information from Objerct to Object

Posted 10 October 2009 - 07:46 AM

There are several issues I notice offhand, I will identify them in your Cow class and you can apply them to Pig as well.

The method moo() doesn't really do anything. It creates an instance of Cow, but doesn't return it, nor does it save to a variable. Based on what your program is supposed to do, I don't think this method is even necessary.

The method name() uses a local variable called name. You create this variable inside the method when you call:
String name if you want to use the global variable within the class Cow you would want to call:
name = "Cow"; instead. There is a hierarchy in methods/classes if the variable is not defined inside the method, Java will check to see if it exists in the class and assign it if it exists.

A second part to the method name(). Even if this change is made, the method name is never called, so the name variable within the class Cow will still be blank. You either need to make the method name a constructor, eg:
public Cow()
		{
			name = "Cow";
		}


or call the method name after creation inside your Animal class:
Cow   moo = new Cow();
moo.name();



Making these changes to both Cow and Pig (with respective names) should get you started in the solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1