• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why generic method in regular class is alway static  RSS feed

 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

I cannot get my head around the reason , why generic method in regular class has to be static.
Anyway, type will be resolved from method execution? How it differs from execution of generic method from generic class?

Update:
I`ve started to suspect, that i`m not properly understand where the border line runtime and compile time.
Please suggest some docs if you have any.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:I cannot get my head around the reason , why generic method in regular class has to be static.

This is not true, methods with type parameters do not necessarily need to be static.

Can you explain why you think this is so, with examples if possible?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:
I cannot get my head around the reason , why generic method in regular class has to be static.


This is simply not true. You can have generic non-static methods in Java.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:
I`ve started to suspect, that i`m not properly understand where the border line runtime and compile time.


The boundary is pretty clear. Compile time is when the program is compiled. And runtime is when the program is executed.

Now, arguably, an IDE can blur it a bit as it takes care of them both for you, so it may help to not use an IDE for a bit.

Also, with just-in-time compilation, you can argue that there is some compilation at runtime. Simply, for this discussion, JIT compilation is considered as done at runtime.

Henry
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, i was confused with Java manual here , where it is the only case , plus i was trying in my IDE and (due to some other error, i understand now) it not allowed me to have non static generic method in regular class.
https://docs.oracle.com/javase/tutorial/java/generics/methods.html

Thanks for the reply !
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:i was trying in my IDE and (due to some other error, i understand now) it not allowed me to have non static generic method in regular class.

Why not, what exactly did you try?

Here's an example that works fine:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:Wow, i was confused with Java manual here , where it is the only case ...

https://docs.oracle.com/javase/tutorial/java/generics/methods.html


From the very first paragraph of the manual that you quoted...

Oracle Java Tutorial wrote:Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.


The manual seems very clear in this regard.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!