views:

530

answers:

6

I'm learning to do some Java 101 syntax stuff. In an exercise I'm trying to do, I can't get the incremented value to print. Any ideas?

Here is my code:

class StaticTest { 
    static int i = 47; 
}
class incrementable { 
    static void increment() { StaticTest.i++; } 
}

class DataOnly { 

    int i;
    double d;
    boolean b; 

    public static void main (String[] args) { 
        incrementable t = new incrementable(); 
        DataOnly df = new DataOnly(); 
        df.i = t.increment(); 
        System.out.println(df.i);
    } 

}

The error I get is:

aTyp0eName.java:18: incompatible types
found: void 
required: int 
df.i = t.increment();

df.i is an int and so is t.increment. I'm guessing it's because increment() is void?

+4  A: 

increment() doesn't have a return value. You'd need to have it return an int for your code to work:

class incrementable { 
    static int increment() { 
        StaticTest.i++;
        return StaticTest.i;
    } 
}

"void" is a keyword reserved for methods which don't return any value. Otherwise, you specify what class or primitive you return (int, double, String, whatever). You can read more about return values here.

Also, for future reference, class names should be capitalized, so you should be using "Incrementable" rather than "incrementable". (In my sample code I kept it the same as you had it so you could just drop the code in for now.)

Daniel Lew
If you return i++, you'll get the value before it was incremented. Probably not what he wanted.
Paul Tomblin
Ah, yes. Fixed.
Daniel Lew
why doensn't i++ return the updated value?
phill
Because you need "return" to return a value from a method. That's just the way Java works.
Daniel Lew
@phill: Because i++ first uses the current value and then increments the variable. In this case, it puts the value in the "return" value of the method. To increment before use "return ++StaticTest.i;"
Leonel Martins
+4  A: 

your method signature is:

static void

Which means nothing is returned from this method.

Hence, attempting to assign increment() to df.i won't work.

toolkit
A: 

also, if you instantiate incrementable, you shouldn't define the method increment() as static. or you can, but don't need to.

you could simply incrementable.increment() if it's static.

just a tip. as for the answer to your question, toolkit already said that right.

rhapsodhy
A: 

Also, your incrementable class should really be Incrementable.

John Topley
is there a reason why it is capitalized?
phill
Just code convention, makes reading "easier" http://java.sun.com/docs/codeconv/index.html
Carlos Heuberger
@phill: Easier to differentiate between types and variables. All ( hopefully ) the java source code you'll see follows this convention. Also method names start with lower case and all names ( except for constants ) use camelCase.
OscarRyz
+2  A: 

The answer is that the increment() method returns a void. That being said, there are a lot of violations of the Java standard syntax that programmers generally use, Namely:

  1. Class names should start with a capital letter.
  2. fields should not be mutated from different classes, rather a helper method should be within the same class
  3. If you want to call a static method, don't reference an instance variable, rather use ClassName.reference
  4. If a variable is only needed locally, it should not be a field (df.i could just be an int i declared within the main method).

EDIT: An example of a helper method within the same class:

class StaticTest { 
    private static int i = 47; //keep i private and mutate and access via methods
    public static void increment() { i++; }
    public static int getI() { return i; }
}
Yishai
could you elaborate on #2--more specifically what would be an example of a helper method?
phill
+2  A: 

The error I get is:

aTyp0eName.java:18: incompatible types
found: void 
required: int 
df.i = t.increment();

df.i is an int and so is t.increment [fault?]. I'm guessing it's because increment() is void?

Eeeexactly, your guess is correct.

This is the error message explained line by line:

aTyp0eName.java:18: incompatible types

You are trying to assign "void" to an int in line 18.

df.i = t.increment();

Here is where the error is.

found: void

You declare what's the return type in the method "signature". The method signature is :

<access modifiers> <return type> <method name> (<parameters> )
static                 void       increment        ()

So the return type is void.

Next line:

required: int

df.i is int as you have previously stated.

So, pretty much you have already your own question answered.

The good point of having a compiler is that it tells you when something is wrong.

The bad thing ( for humans ) you have to learn to read those messages. They vary from programming language to another and even from compiler to compiler.

This would be a corrected version:

class StaticTest { 
    static int i = 47;  
}
class Incrementable { 
    static int increment() { 
        return ++StaticTest.i;
    } 

}

class DataOnly { 

    int i;
    double d;
    boolean b; 

    public static void main (String[] args) { 
        Incrementable t = new Incrementable(); 
        DataOnly df = new DataOnly(); 
        df.i = t.increment(); 
        System.out.println(df.i);
    } 

}

There are some other enhancements could be done, such as adding access modifiers to the methods and attributes, but for now I think this would help.

OscarRyz