Generics is one of the most elemental concepts of Core Java.
Whenever we talk about Generics, the leading thoughts that sprout in our mind are Type Parameter, Type Erasure etc.
To be honest, I always find Generics concepts a little hard to grasp at the very first go, but once we start adapting it, it is becomes more and more interesting and very useful.
This post is not intended to deliver any tutorial, or any introduction to basics of Generics, but about scenario, which I confronted recently and I think I should bring it into light.
We all more or less at some point in our coding lifetime, have encountered Generics, especially while using Collection Framework elements (Maps, Lists, Sets etc.)
With all these elements we basically use the instance methods of the Objects, but what about when invoking static Methods.......
Lets describe it with an example:
But what about we introduce a static method and try to access the references of type T within it,
Now, the compiler will now exhibit its dislike and the error we will get is Cannot make a static reference to the non-static field ...., which is obvious..
So it becomes:
So to get rid of the error, the first thing that we will do is:
And after analysis I found this to be absolutely logical, as the Type parameter are belongings of instances of class XYZ, whenever we create an instance of the class, Type parameter will be bound to that object so cannot be conferred a static context, as making it static means the Type Parameter belongs to the Class, irrespective of the objects created, which is absolutely not!!!!!!
So I found the reason, but the actual problem still persists, as how to use a Type Parameter within a static Context and the Answer is just declare the Type Parameter Here "M" between the static modifier and the return type of the static method and we are free to use it within the static context, like
or Class
Now how to call the static Method????!!!!!
Seems pretty Ordinary Right.....what is so particulat in calling a static method of a class.
The answer is:
and the Answer is:
Great isn't it!!!!!
Now its time to introduce all of you to the another awaited member of the family the XYZBuilder
Its definition is:
The Test class looks alike:
****** In BuildStatic
class java.lang.Object
The Payload type is:class java.lang.String
So Generics is really intriguing.
Please share your views or any other GENERICS feature which you consider to be appealing.
So keep Coding, keep contributing.
Whenever we talk about Generics, the leading thoughts that sprout in our mind are Type Parameter, Type Erasure etc.
To be honest, I always find Generics concepts a little hard to grasp at the very first go, but once we start adapting it, it is becomes more and more interesting and very useful.
This post is not intended to deliver any tutorial, or any introduction to basics of Generics, but about scenario, which I confronted recently and I think I should bring it into light.
We all more or less at some point in our coding lifetime, have encountered Generics, especially while using Collection Framework elements (Maps, Lists, Sets etc.)
With all these elements we basically use the instance methods of the Objects, but what about when invoking static Methods.......
Lets describe it with an example:
public class XYZThe class XYZ has a Type Parameter T and we have declared two references of Type T and have accessed them in a instance method. Upto this point the compiler is very Happy and doesn't seems to be complaining.{ T t1; T t2; public void build() { System.out.println(" ****** In Build ******* "+((t1 != null)?t1.toString():"null")+"-----"+(t2 != null?t2.toString():"null")); } }
But what about we introduce a static method and try to access the references of type T within it,
Now, the compiler will now exhibit its dislike and the error we will get is Cannot make a static reference to the non-static field ...., which is obvious..
So it becomes:
public staticOooops!!!!! Sorry, I forgot to introduce another member XYZBuilder, I will don't worry.....XYZBuilder buildStatic(Class m) { System.out.println(" ****** In BuildStatic"+t1); return new XYZBuilder (m); }
So to get rid of the error, the first thing that we will do is:
static T t1;and I too did the same thing, but will my surprise, I found that it did not worked, and instead I accompanied myself with the same error, which is: Cannot make a static reference to the non-static field ....
And after analysis I found this to be absolutely logical, as the Type parameter are belongings of instances of class XYZ, whenever we create an instance of the class
So I found the reason, but the actual problem still persists, as how to use a Type Parameter within a static Context and the Answer is just declare the Type Parameter Here "M" between the static modifier and the return type of the static method and we are free to use it within the static context, like
public staticWe have used the wildcard character ? within the method for flexibility so that we can pass Class of any Type, but we can also restrict this with ClassXYZBuilder buildStatic(Class m) { System.out.println(" ****** In BuildStatic"); return new XYZBuilder (m); }
Now how to call the static Method????!!!!!
Seems pretty Ordinary Right.....what is so particulat in calling a static method of a class.
The answer is:
XYZ.buildStatic(Object.class);but this is the call using Raw, where is the Type Parameter ???, that we have declared in the static Context,
and the Answer is:
XYZBuilderThe part to look at is the XYZ.<String>buildStatic(Object.class);xyzBuilder = XYZ. buildStatic(Object.class);
Great isn't it!!!!!
Now its time to introduce all of you to the another awaited member of the family the XYZBuilder
Its definition is:
public class XYZBuilderWith this XYZBuilder class, we will be able to exhibit the utility of the Type Parameter "M" introduced within the static context. From buildStatic() method we are returning an instance of XYZBuilder, with a Type Parameter, this Type Parameter is the type of payload, in XYZBuilder class, so whatever Type we pass in the static context, that becomes the integral part of XYZBuilder class, and we have captured the Class type thouugh constructor in another variable for Record purpose only.{ T payload; Class t2; XYZBuilder() { } /*XYZBuilder(Class t) { this.t2 = t; }*/ XYZBuilder(Class t) { this.t2 = t; } public XYZBuilder payload(T payload) { this.payload = payload; System.out.println("The Payload type is:"+payload.getClass()); return this; } public XYZ build() { return new XYZ (); } }
The Test class looks alike:
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//new XYZ().build();
XYZBuilder xyzBuilder= XYZ.buildStatic(Object.class);
System.out.println(xyzBuilder.t2);
xyzBuilder.payload("okkkk");
//XYZ.buildStatic(Object.class);
}
}
The output will be:****** In BuildStatic
class java.lang.Object
The Payload type is:class java.lang.String
So Generics is really intriguing.
Please share your views or any other GENERICS feature which you consider to be appealing.
So keep Coding, keep contributing.