这样:
public class Hello {
String str = "Hello,world!";
}第一次吧,代码写在那儿?

解决方案 »

  1.   

    public class Hello{
        public static void main(String[] args){
            String str;
    str = "Hello,world!";        System.out.println(str);
    }
    }
      

  2.   

    提示以下错误:Hello.java:3: <identifier> expected
    str = "Hello,world!";1 error
      

  3.   

    public class Hello{
        public static void main(String[] args){
             String str;
    str = "Hello,world!";
             System.out.println(str);
    }
    }
      

  4.   

    同样一楼的,也可以是:
    public class Hello {
    String str;
    {
    str = "Hello,world!";
    }
    }
      

  5.   

    public class Hello {
    String str;
    {
    str = "Hello,world!";
    }
    }ORpublic class Hello{
        String str;
        public static void main(String[] args){         
    str = "Hello,world!";
    }
    }
      

  6.   

    to: kenli(致虚子)
    --
    为什么加了{}就可以了呢,可以帮忙解释一下吗?谢谢!
      

  7.   

    因为你原来的
    str = "Hello,world!";
    是一个可执行的语句,不是定义语句。可执行语句必须包含在一个方法或者函数里。。不能在类的定义里
      

  8.   

    public class Hello {
      public static void main (String[] args) {
        String str = "Hello,world!";
        System.out.println(str);
      }
    }
      

  9.   

    谢谢,不过我还是没有明白,为什么
    public class Hello {
    String str;
    str = "Hello,world!";
    }
    不行,现在我只要原因,其他不要,只要告诉我为什么不能够用就可以了,谢谢。
      

  10.   

    还不明白?
    str = "Hello,world!";
    所处位置是定义数据类型的地方!!!
      

  11.   

    public class Hello {
    String str;
    {
    str = "Hello,world!";
    }
    }
    ---
    那{}外面居然没有方法的名字?是空的还是怎么的?他会执行吗?
      

  12.   

    干嘛用那么诡秘的方法呢!? 正正规规的方法不用? 
    public class Hello {
    String str = new String("Hello,world!");
    // str = "Hello,world!";
    }
      

  13.   

    这样的方法还是有一点用处的。如果前边加了static,表示在类的初始化的同时执行。这样有利于初始化一些不是很方便的东西,例如Array, Vector等等。可以使代码更清晰化。我想加了{}可以认为是匿名的方法。 Class/Object Initialize的时候会调用。
      

  14.   

    要实例话字符串,一种是直接用引号引起来另一种是用String类提供的方法
      

  15.   

    public class Hello {
    String str = null;
    str = "Hello,world!";
    }
    应该可以!!
    局部变量定义时要赋初值.
      

  16.   

    面向对象,很重要的一个概念是class
    ^_^
      

  17.   

    这个程序的错误就在于只有class也就是类,而没有方法,所以会报错
    每个类里最少要有一个方法.
      

  18.   

    你的类没有入口方法,写才这样才能编译public class Hello {
      public static void main (String[] args) {
        String str = "Hello,world!";
        System.out.println(str);
      }
    }
      

  19.   

    引用:fantasyCoder(牛仔+T恤) 
    语法规则!
    有必要这样刨根问底吗?
    省点时间学点别的!
    不要打击别人好不好,如果这个程序在Think in java   中提出来,您还会这么说吗
      

  20.   

    public class test
    {
    String str="Hello";
    }
    这样也行:)
      

  21.   

    要不这样:
    public class Hello {
      String str;
      public Hello() {
        str = "Hello,world!";
      }
    }