Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Classes' - 7 code snippet(s) found

 Sample 1. Class Nested within Interface / Static Inner class within Interface

public interface SampleInterface {
   public int sampleMethod(List sampleList);

   static class Impl implements SampleInterface {

      public int sampleMethod(List sampleList) {
         return 0;

   Like      Feedback     nested classes  nested class  inner class  inner classes  static inner class  static inner class within interface  @override

 Sample 2. Declaring Abstract Class

public abstract class TestClass {
public static void main(String[] args){

   Like      Feedback     abstract classes  main method declaration  main method

 Sample 3. Class bean with getter , setter and constructor

public class Employee {
public String name;
public int age;
public int salary;

Employee(String name, int age, int salary) { = name;
this.age = age;
this.salary = salary;

public String getName() {
return name;

public void setName(String name) { = name;

public int getAge() {
return age;

public void setAge(int age) {
this.age = age;

public int getSalary() {
return salary;

public void setSalary(int salary) {
this.salary = salary;

   Like      Feedback     bean   pojo   plain java objects   getters   setters   getters nd setters   classes   class   constructor   parameterized constructor  this keyword

 Sample 4. Database Api using java.sql classes

package BuggyBread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseAPI {

   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "";

   // Database credentials
   static final String USER = "";
   static final String PASS = "";

   Connection conn = null;
   public void initializeConnection(){
      try {
         // STEP 2: Register JDBC driver

         // STEP 3: Open a connection
         System.out.println("Connecting to database...");
         conn = DriverManager.getConnection(DB_URL, USER, PASS);

      } catch (SQLException se) {
         // Handle errors for JDBC
      } catch (Exception e) {
         // Handle errors for Class.forName
      } finally {
         try {
            if (conn != null)
         } catch (SQLException se) {
         }// end finally try
      }// end try

   Like      Feedback     database api  java.sql

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
 Sample 5. Get the properties of class (i.e package name , interfaces , subclasses etc ) using ClassUtils( Apache Commons )

Class class1 = ClassUtils.getClass("BuggyBreadTest"); 

   Like      Feedback     ClassUtils  Apache Commons  Get Class Properties  java.lang.Class

 Sample 6. Code Sample / Example / Snippet of org.apache.bcel.generic.ClassElementValueGen

    public void testCreateClassElementValue() throws Exception


final ClassGen cg = createClassGen("HelloWorld");

final ConstantPoolGen cp = cg.getConstantPool();

final ObjectType classType = new ObjectType("java.lang.Integer");

final ClassElementValueGen evg = new ClassElementValueGen(classType, cp);

assertTrue("Unexpected value for contained class: '"

+ evg.getClassString() + "'", evg.getClassString().contains("Integer"));

checkSerialize(evg, cp);


   Like      Feedback      org.apache.bcel.generic.ClassElementValueGen

 Sample 7. Code Sample / Example / Snippet of org.apache.bcel.classfile.InnerClasses

public Attribute copy(final ConstantPool _constant_pool) {
final InnerClasses c = (InnerClasses) clone();
c.inner_classes = new InnerClass[inner_classes.length];
for (int i = 0; i < inner_classes.length; i++) {
c.inner_classes[i] = inner_classes[i].copy();
return c;

   Like      Feedback      org.apache.bcel.classfile.InnerClasses  object initialization using clone

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner