The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial: Generics in java [How to define a Generic method which accepts user-defined class]

0 replies.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial: Generics in java [How to define a Generic method which accepts user-defined class] Posted: May 16, 2017 11:08 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial: Generics in java [How to define a Generic method which accepts user-defined class]
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE


Click here to watch in Youtube :
https://www.youtube.com/watch?v=iDFnRvIwzUI&list=UUhwKlOVR041tngjerWxVccw

Bird.java
public class Bird
{
private String birdName;

public String getBirdName()
{
return birdName;
}

public void setBirdName(String birdName)
{
this.birdName = birdName;
}

}
Employee.java
public class Employee
{
private String name;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

}
GenericDemo.java
public class GenericDemo
{
public static void main(String[] args)
throws IllegalAccessException, InstantiationException
{

Bird bird = getInstance(Bird.class);
System.out.println(bird);

Employee employee = getInstance(Employee.class);
System.out.println(employee);

}

/*
* Class objects can be used as type specifications too, at runtime.
*/

public static <T> T getInstance(Class<T> theClass)
throws IllegalAccessException, InstantiationException
{

return theClass.newInstance();
}
}
Output
Bird@58d25a40
Employee@726f3b58
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsMethodDemo_Bird_emp_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/GenericsMethodDemo_Bird_emp_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/GenericsMethodDemo_Bird_emp_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Tutorial: Generics in java [How to define a Generic method which accepts user-defined class]


    Topic: JVM Statistics with jstat Previous Topic   Next Topic Topic: Handling Events in React

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use