Share

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 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) {
this.name = name;
this.age = age;
this.salary = salary;
}

public String getName() {
return name;
}

public void setName(String name) {
this.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 2. Class Nested within Interface / Static Inner class within Interface

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

   static class Impl implements SampleInterface {

      @Override
      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 3. Declaring Abstract Class

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

   Like      Feedback     abstract classes  main method declaration  main method


 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
         Class.forName("com.mysql.jdbc.Driver");

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

         conn.close();
      } catch (SQLException se) {
         // Handle errors for JDBC
         se.printStackTrace();
      } catch (Exception e) {
         // Handle errors for Class.forName
         e.printStackTrace();
      } finally {
         
         try {
            if (conn != null)
               conn.close();
         } catch (SQLException se) {
            se.printStackTrace();
         }// 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 Buggybread.com
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"); 
System.out.println(ClassUtils.getPackageName(class1));
System.out.println(ClassUtils.getAllInterfaces(class1));

   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();

}

c.setConstantPool(_constant_pool);

return c;

}


   Like      Feedback      org.apache.bcel.classfile.InnerClasses



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



comments powered by Disqus