Java 'this' keyword

I am just starting to program, and I would like to do an exercise from a book, but I can’t. What is my problem:

public class increment {
    int increment() {
        return this + 1; // aka this++
    }

    public static void main(String[] args) {
        int a = 0;
        System.out.println(a.increment());
    }
}

As you probably already guessed that this does not work, I want to ask you how to get the resulting integer, incremented by one, but using the keyword 'this'.

Regards and sorry for the stupid questions.

+3
source share
8 answers

this- object (current object). You cannot "enlarge" it.

The way to do this is:

public class Increment {
    int a = 0;
    int increment() {
        return a + 1; 
        // or: return this.a + 1;
        // or: a++; return a; if you want a to be incremented from now on
    }

    public static void main(String[] args) {
        Increment inc = new Increment();
        System.out.println(inc.increment());
    }
}
+4
source

It is strange to name a class as a method. I think you wanted this:

public class Counter {

int val;

 public Counter (int start) {
   val = start;
 }
 public void increment() {
    val ++;
 }
 public String toString () {
   return Integer.toString (val);
 }

 public static void main(String[] args) {
    Counter counter = new Counter (0);
    counter.increment ();
    System.out.println(counter.toString ());
 }
}
+6
source

this Java . , .

a increment, int. .increment(), , increment.

, , , .

public class Increment { //Java likes capitalized class names
    private int myInt;    

    public Increment(int a) { //constructor
        myInt = a;
    }

    public int increment() {
        return ++myInt;
    }    

    public static void main(String[] args) {
        Increment a = new Increment(0);
        System.out.println(a.increment());
    }

}

increment, . increment , .

+2

. -, .

public class Test {
    private int var;

    public Test(int i) {
        this.var = i;
    }

    int increment() {
       this.var++;
    }
}

public static void main(String[] args) {
    Test t = new Test(0);
    System.out.println(t.increment());

}
0

+ (this). java.

0

Something like this will work:

class MyInteger {
    private int internal;

    public MyInteger( int value ){
        this.internal = value;
    }

    public int incerment(){
        return ++this.internal;
    }
}

public class Increment {

    public static void main(String[] args) {
        MyInteger a = new MyInteger(0);
        System.out.println(a.increment());
    }
}

You see, you can only apply methods to your classes, not to existing classes or to primitives of type int.

0
source

I don't think you can use this to return a value, except that you create a new class as follows:

class Increment1
{
    private int a;
    public int increment2(int a)
    {
        this.a=a;
        return this.a + 1;
    }
}

public class Increment
{

    static Increment1 b = new Increment1();

    public static void main(String[] args)
    {
        int a = 0;
        System.out.println(b.increment2(a));
    }
}
0
source

This applies to the current instance of the class, not to a specific member.

You want to increase the property (I assume the type is long or int), and not an instance of your increment class (should, by the way, Increment).

Something like this will work:

public class increment {

private int innerValue = 0;

int increment() {
    innerValue+=1
    return innerValue; // aka this++
}

public static void main(String[] args) {
    increment a = new increment()
    System.out.println(a.increment());
}
}
-1
source

All Articles