Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Wrong code

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Wrong code

    The files at the links below contain source code for three interrelated classes, a Dog class, a DogOwner class, and a DogTester class that manipulates objects from the other two classes.

    /JavaCS1/src/dogpatch/DogTester.java
    /JavaCS1/src/dogpatch/DogOwner.java
    /JavaCS1/src/dogpatch/Dog.java

    Write a statement that creates a 15 kg showdog called "Hotdog" that's a poodle. Reference this dog using the Dog variable d.


    public class DogTester {
     
      public static void main (String[] arg) {
        Dog d = null;

    SO i write:
    Dog d = new Dog("Hotdog","poodle",15.0,true);

    and then there error says "compilation error(line 1, column 5): d is already defined in q1()

    i dont understand because its saying use d then it says its already defined..
    Last edited by whattheeff; March 4th, 2011 at 05:17 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Wrong code

    Your links don't work.

    I think somewhere else you must have defined it already.

    Having code like
     
    Dog d;
     
    lines of code in between
     
    Dog d = new Dog(params);

    would cause an error like that.

    Can't say for sure without seeing the code.

    -----Edit----

    Yep, I can see it now from the code you posted here(still can't see links):
    Dog d = null;
     
    Dog d = new Dog("Hotdog","poodle",15.0,true);

    Change that to:

    Dog d = null;
     
    d = new Dog("Hotdog","poodle",15.0,true);

    Dog d = null;

    means you're declaring an object of type Dog called d and having it be null

    Dog d = new Dog("Hotdog", "poodle", 15.0, true);

    Is trying to make a Dog object called d and initializing it to those values, but there is already a Dog object called d.
    Last edited by javapenguin; March 4th, 2011 at 05:27 PM. Reason: Found cause fo error

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    whattheeff (March 4th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Wrong code

    thanks man that worked.. simpler then i thought haha damn

Similar Threads

  1. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  2. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  3. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 09:57 AM