• Register
0 votes

Problem :

In Java, I want to have something as below :

class MyClazz<T> {
  static void doIt(T object) {
    // More code

But I face below error

“Cannot make a static reference to the non-static type T”

I am unable to understand generics beyond a basic uses and thus unable to make much sense of that. It doesn't help that I wasn't able to find the much info on a internet about a subject.

Could someone clarify if such the use is possible, by the similar manner? Also, why was my original attempt went unsuccessful?

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

You should not use the class's generic type parameters in the static methods or static fields. A class's type parameters are only in the scope for instance methods and the instance fields. For a static fields and static methods, they are shared among all of the instances of the class, even instances of different type of parameters, so obviously they cannot depend on the particular type parameter.It does not seem like the problem should require using a class's type parameter.

The better way to handle the things is to put a type parameter in a method itself:

static <U> void doIt(U object)

Hope it fixes your issue.

9 7 4
38,600 points

Related questions

1 vote
1 answer 27 views
Problem: I am very noob in java programming. I was doing my coding practice and wrote this code: public class StaticDemo { int i = 0; public static void main(String[] args) { System.out.println("in main method which is static"); // Trying to access ... -static method instanceMethod() cannot be referenced from a static context instanceMethod(); ^ 3 errors Can anyone help me out to fix this problem?
asked Apr 7, 2020 LizzyM 6.1k points
1 vote
1 answer 6 views
Problem; Getting the following error. Java non static variable this cannot be referenced from a static context.
asked Mar 29 PkGuy 23.5k points
0 votes
2 answers 12 views
Problem: Please help me &hellip; how can I solve it? How to call a static method java?
asked Apr 3 Ifra 34.7k points
0 votes
1 answer 21 views
0 votes
1 answer 7 views
Problem: Could someone please explain to me in really simple terms what Static or Class Methods are?
asked Apr 22 ummeshani 8.8k points
0 votes
1 answer 3 views
Problem: I'm designing a class capable of deserializing some file, and I'm wondering what would be the implications of this two options.How do this two options compare? There would be any difference? Are there any notable points to keep in mind when evaluating the design?
asked 2 days ago sumaiya simi 43.9k points
0 votes
1 answer 5 views
Problem: When to use generic methods and when to use wild-card? Can someone put light on this area?
asked Apr 23 ummeshani 8.8k points
0 votes
1 answer 30 views
Problem: I am unable to understand this problem .. Please suggest me a better solution to solve it .. generic parameter 't' could not be inferred
asked Apr 6 Ifra 34.7k points
0 votes
1 answer 8 views
Problem: What are the desirable situation (real-life examples) to create static methods except for creating helper? Can anybody answer this question by briefly explaining every scenario???
asked Mar 23 ummesalma 29.2k points