This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Invoke method without instance via constructor in Java  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a great doubt as to what this statement is called

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



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 is the name of this instruction or statement.

Thank you for your attention.

P.D : Sorry .. I'm learning
 
Ranch Hand
Posts: 31
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically it creates a new Random object and calls a method nextInt on this object. It doesn't keep a reference to the object which is probably why you got confused.
 
Radwulf Candle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:Basically it creates a new Random object and calls a method nextInt on this object. It doesn't keep a reference to the object which is probably why you got confused.



Thank you!

I know perfectly its operation and utility but I don't know his technical name.

I just want to know if this instruction has a technical name to know how to document it.

I want to know if this has an anonymous class name, instance variable, magic instruction or something else.

Thank you for your patience.
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware of a specific name for it. It's kind of like "method chaining" but not exactly because you're chaining with a constructor call. It's less like an anonymous class because you're not actually creating a different class, just a reference to an object that you immediately use and throw away.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Instance variable" - no

"magic instruction" - sounds cool but no. On second thought, it is scary because any technology that is sufficiently advanced and cannot be understood is often considered as magic. Being as you said you thought you understood the operation, I hope you don't still consider it as magic.
 
Marshal
Posts: 61723
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't say, “constructor methd,” because constructors aren't methods.
It is a simple method call on a throwaway object, created in the same statement.
 
Radwulf Candle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:"Instance variable" - no

"magic instruction" - sounds cool but no. On second thought, it is scary because any technology that is sufficiently advanced and cannot be understood is often considered as magic. Being as you said you thought you understood the operation, I hope you don't still consider it as magic.




Hi !!

It was just a funny way to call this instruction, that's what I mean, I don't know how to name this to put it in the documentation.

I thought it would be fun as a joke.
 
Campbell Ritchie
Marshal
Posts: 61723
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it isn't a statement; it is a declaration and initialisation of the int.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Actually, it isn't a statement; it is a declaration and initialisation of the int.


The JLS calls it a LocalVariableDeclarationStatement: https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.4

So I think that would make it a statement.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Radwulf Candle wrote:I don't know how to name this to put it in the documentation.


What "documentation" are you trying to write? I hope you don't mean your code comments. Comments in code should explain WHY you are doing something, not how or what you're doing.
 
Radwulf Candle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Radwulf Candle wrote:I don't know how to name this to put it in the documentation.


What "documentation" are you trying to write? I hope you don't mean your code comments. Comments in code should explain WHY you are doing something, not how or what you're doing.



The documentation is for me, they are my notes in case one day I have to consult it or if I want to share it in some blog that I do.

Thank you for your help.
 
Campbell Ritchie
Marshal
Posts: 61723
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . The JLS calls it a LocalVariableDeclarationStatement . . .

So I think that would make it a statement.

Yes, it would. Thank you
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Radwulf Candle wrote:The documentation is for me, they are my notes in case one day I have to consult it or if I want to share it in some blog that I do.

Thank you for your help.


No problem. I would just write something like this:

Since the result of  the new operator is a reference to a new object, you can use the reference directly and invoke a method on that object. This means that you can create a one-time use object, invoke one of its methods, and then immediately mark the object for disposal by the garbage collector. For example, you can write this:

This statement first creates a new instance of Random and then immediately invokes that new object's nextInt() method. The result returned by nextInt(100) is then assigned to the oneRandomNumber variable. Since the reference to the new Random object was not assigned to any variable, it immediately becomes eligible for garbage collection from that point on.


In fact, if there's a "technical name" you can give this kind of statement, I think "a one-time use object reference" would probably fit the bill best.
 
Radwulf Candle
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Radwulf Candle wrote:The documentation is for me, they are my notes in case one day I have to consult it or if I want to share it in some blog that I do.

Thank you for your help.


No problem. I would just write something like this:

Since the result of  the new operator is a reference to a new object, you can use the reference directly and invoke a method on that object. This means that you can create a one-time use object, invoke one of its methods, and then immediately mark the object for disposal by the garbage collector. For example, you can write this:

This statement first creates a new instance of Random and then immediately invokes that new object's nextInt() method. The result returned by nextInt(100) is then assigned to the oneRandomNumber variable. Since the reference to the new Random object was not assigned to any variable, it immediately becomes eligible for garbage collection from that point on.


In fact, if there's a "technical name" you can give this kind of statement, I think "a one-time use object reference" would probably fit the bill best.



Thank you for your time and patience.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One final thing before I shut up: Fluent APIs, which I referred to earlier in this thread, often include self-referencing methods. That is, methods that return a reference to the current object. This is common in patterns like the Builder, which allows you to do things like this:

and
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!