Nico B.
Nico B.

Reputation: 13

How to define ascending Intergers in function?

My question is rather simple but nonetheless I wasnt able to find an answer. Id like to create a new Integer (or any other data type) every time a method is called like:

public void x(){
    int i = 0;
    int num;(plus value of i in its name eg: num1, num2, num3,...)
    i++;
    }

Upvotes: 0

Views: 54

Answers (2)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

You should store your int num into the class instead of the method, this wise your variable has object escope and will have its reference anytime you call your method.

Upvotes: 0

Nick
Nick

Reputation: 595

what you're thinking of is called varvars (variable variables) and Java does not support them (thank god - we have reflection) PHP does though: http://php.net/manual/en/language.variables.variable.php

what you probably want is to put your Integers in a Collection: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Upvotes: 1

Related Questions