import java.util.List;
import java.util.Set;
public class EquTypeDao {
StringBuilder sb=new StringBuilder();
sb.append("[");//这一行出错,为什么? public String get(){
StringBuilder sb=new StringBuilder();
sb.append("[");//这一行可以用 }
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();//这里也可以用
sb.append("[");
sb.append(" ");
sb.append("]");
System.out.println(sb.toString());
}

}
上面是我写的代码,主要想用一下StringBuilder,可是在EquTypeDao里不能使用,但在main,get里却可以用,不知道是什么原因,希望大家帮我解决一下。谢谢。

解决方案 »

  1.   

    我们刚学这个,但是上面出错与StringBuilder无关!
    注意:你让一个对象去执行某个方法必须放在方法体里面!
    要知道:类里面只有方法(行为)和属性(成员变量)!
    StringBuilder sb=new StringBuilder(); //属性
    public static void main(String[] args) //方法
    public String get()//方法
      

  2.   


    public class Test {
    {
    StringBuilder sb=new StringBuilder(); 
    sb.append("[");//这样没错
    System.out.println(sb.toString());
    }
    public static void main(String[] args){
    new Test();
    }
    }
    用大括号括起来就可以了
      

  3.   

     似乎你给他赋值就可以了
     例如:
     你出错的地方改为:
    String str = sb.append("[");// 这样就可以饶过编译器
     也达到了你自己想要的结果
      

  4.   


    恩,确实输出了,昨天可能没注意到那么象这种,代码块的情况,在实际项目中,会怎么运用??wuhaozhiyuan?谢谢
      

  5.   

    import java.util.List; 
    import java.util.Set; 
    public class EquTypeDao { 
    StringBuilder sb=new StringBuilder(); 
    sb.append("[");//这一行出错,为什么? 
    //类体内只能有属性和方法public String get(){ 
    StringBuilder sb=new StringBuilder(); 
    sb.append("[");//这一行可以用 } 
    public static void main(String[] args) { 
    StringBuilder sb=new StringBuilder();//这里也可以用 
    sb.append("["); 
    sb.append(" "); 
    sb.append("]"); 
    System.out.println(sb.toString()); 
    } } 
      

  6.   


    在没有public构造器的静态类中,需要对一些属性进行初始化,
    这时就会用到静态代码块
    例如:hibernate的session工厂中package com.sessionFactory;import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;/**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private HibernateSessionFactory() {
        }

    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        } /**
         *  Rebuild hibernate session factory
         *
         */
    public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    } /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
                session.close();
            }
        } /**
         *  return session factory
         *
         */
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    } /**
         *  return session factory
         *
         * session factory will be rebuilded in the next call
         */
    public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
    } /**
         *  return hibernate configuration
         *
         */
    public static Configuration getConfiguration() {
    return configuration;
    }}