2

I have a great doubt as to what this action is called and how it is administered in memory.

Inside the main() method I make these sentences or instructions.

public static void main(String[] args) {
    int i = 0;
    int j = new Random().nextInt(100); // As it is called this way of acting or as it is called.
}
  • I have clear that what it does is directly invoke the Random class constructor method, invoke the nextInt method and generate a random number that is stored inside the int j variable but I don't know how to define this type of action and I don't know if it is correct to do this kind of instructions.

  • I'm curious to know what this type of action is called.

Thank you for your attention.

P.D : Sorry .. I'm learning

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
Raúl Vela
  • 49
  • 7

3 Answers3

5
int j = new Random().nextInt(100);

is almost the same as

Random r = new Random();
int j = r.nextInt(100);

i.e. both create an instance (object) of the Random class, and then call a method of that instance.

The difference is that in the first case, you don't keep a reference to the created instance, so you can't access that instance again, and since no reference to that instance exists, it can be immediately garbage collected.

As Andy suggested, you can look at it as if you created an instance of the Random class and assigned it to a variable, called the nextInt() method, and then exited the scope in which that variable was declared:

int j;
{
    Random r = new Random();
    j = r.nextInt(100);
}
// at this point you have no access to the Random instance
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    You might say that it's more like `int j; { Random r = new Random(); j = r.nextInt(100); }`. – Andy Turner Nov 18 '18 at 11:24
  • @Eran Thank you very much I understand this explanation but I want to know the name that receives this type of statement... anonymous class, direct instance without variable instance ? I do not know what name has to refer to it. – Raúl Vela Nov 18 '18 at 16:24
  • @Sin I'm not sure there's a special term for that. It's not an anonymous class. – Eran Nov 18 '18 at 16:59
  • @Eran okey ! thank you , I was very interested in knowing what name this type of statement has. – Raúl Vela Nov 18 '18 at 17:40
2

One important note: this code is a bit shorter, and it's OK if you will not need to call nextInt() again. If you will, you better store the instance on Random class in a variable because the process of creating multiple Random objects for multiple int values is TOO heavy.

Dima Rich
  • 61
  • 6
1

Well technically there is an instance of the class, since you see the keyword new.

What you see here is called method chaining.

You first call the constructor with new Random() and then chain the nextInt() method to it.

if you are curious how can you call a method without an instance of a class, the simple answer is you need a static class :) a good reference on static classes

But a simple example would be the Math class in java, with it you can do this:

double floor = Math.floor(7.343); 

notice how you dont use "new" when invoking Math

thefolenangel
  • 972
  • 9
  • 29