0
class Myclass{
    int x; 
    Myclass (int i){
        x = i;
    }
}

That is the code I have in my book. I wanted to know if this code would work?

class Myclass{
    int x;
    Myclass (x)
}

I also could try

class Myclass{
    int x;
    Myclass (int x)
}

in response to my first answer...would this work?

6
  • try it... It's easy to test that and I don't see why you would do that anyways. Commented Sep 6, 2013 at 0:37
  • I can't get java to work on my computer or I would have a lot more fun learning from this book. Commented Sep 6, 2013 at 0:39
  • @user2752603 I have formatted your code properly. For future reference, you can format your code by highlighting the code and clicking the "{}" button. Additionally, any lines indented four spaces is formatted as code. Commented Sep 6, 2013 at 0:39
  • thanks, didn't know to space the first line with 4 spaces, i got it now i appreciate it Commented Sep 6, 2013 at 0:41
  • What happened with your Java installation? DId you install a JDK? Did you set the JAVA_HOME environment variable to your installation directory? Did you add %JAVA_HOME%\bin to your PATH (or $JAVA_HOME/bin, if you use OSX or Linux)? Commented Sep 6, 2013 at 0:46

4 Answers 4

1

The latter code would not work because in Java you have to explicitly state the type. (There are no implicit type declarations)

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

1 Comment

Yes, it would work. The first line of your constructor would most likely be this.x = x;
0

You may have a parameter or local variable with the same name as an instance variable, yes. In that case, the parameter or local variable will shadow the instance variable. To refer to the instance variable in such a case, use:

this.x

For example, it's common to see this pattern:

class MyClass {
    private int x;

    public MyClass(int x) {
        this.x = x;
    }
}

Note that, as Josh M points out, it is not possible to omit the type. If that is what your question was about, then no, you may not.

Comments

0

This will compile:

class Myclass {
    int x;
    Myclass (int x) {
    }
}

However, when you do this you end up with two variables with the same name, the instance variable x, which can be referred to explicitly inside the constructor (or any other class method) as this.x, and the local parameter variable x local to the constructor. If you just refer to x in the constructor you'll get the local one. This is referred to as variable shadowing.

Even though you've decided to give these two variables the same names in your source code, in the code the compiler produces they are completely unrelated. You might as well have named the parameter y.

An experiment to try that might help understanding this is to give the variables different types. Make your instance variable a boolean, for instance. Then you can try different things and see that they really are completely different variables that just happen to have the same name.

Comments

0

After @Chris Hayes' and @Samuel Edwin Ward's answers, here's one trick.

class MyClass {
    private int x;

    public MyClass(final int x) {
        this.x = x;
    }
}

The final modifier tells the compiler that that x cannot be modified. If you accidentally write

class MyClass {
    private int x;

    public MyClass(final int x) {
        x = x;  // Oopsie!
    }
}

the compiler will complain.

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.