2

I am kind of embarrassed about this, and definitely I am just forgetting something simple.

if this is body.java

public class body{

    public static void main(String args[])
    {
        int i = 0;
     part aPart = new part(i);
     aPart.add();
    }
}

and this is part.java

public class part{

    private int i;
    public int part(int i)
    {
        this.i = i+10;
    }
    public add ()
    {
        i = i++;
        System.out.println(i);
}

Why when i run javac to compile body.java, it says unknown symbol for part?

3
  • Are these in the same package? Commented Nov 16, 2010 at 17:58
  • If you'd like something that will take care of compilation for you, and you want something simpler and lighter than Eclipse, check out drjava.org. Commented Nov 16, 2010 at 18:03
  • 2
    Always define the first character in a Java class in upper case (Body, Part) Commented Nov 16, 2010 at 18:04

5 Answers 5

4

because part is your constructor (you don't declare the return type as @amir said in his answer). You should do

public part(int i) {...}

as a note, Java convention is to have class names capitalized, so you should change your file to Part.java, your class declaration to "Part", and your constructor too...

EDIT -- @coolbeans answer is correct too -- if your code in the question is correct, you are missing a closing brace.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, i noticed the capitalization issue. I'm having the problem in a much bigger program, so i thought it better to try and make this test program to see what i forgot. I found the issue though, after comparing, I was making edits and made one too many!
1

javac *.java?

1 Comment

That is the immediate problem... but the part constructor is also wrong
1

Change your Part class like below:-

public class Part{

    private int i;
    public Part(int i)
    {
        this.i = i+10;
    }
    public void add()
    {
        i = i++;
        System.out.println(i);
    }
}

And call it this way:-

int i = 0;
Part aPart = new Part(i);
aPart.add();

1 Comment

thanks, that helped me find the problem in my bigger program i was editing.
0

And to elaborate on what hvgotcodes said, constructors do not have a return type. The constructor of a Java class is not an ordinary method. It's sole purpose is to instantiate an object of the the class which it belongs to.

1 Comment

i referenced your answer in my answer, if that is OK with you.
0

You need to declare the constructor

public part(int i) {
   this.i = i;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.