Tutorial :Java: Can't get incremented value to print.



Question:

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?


Solution:1

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.


Solution:2

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.)


Solution:3

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.


Solution:4

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; }  }   


Solution:5

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.


Solution:6

Also, your incrementable class should really be Incrementable.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »