静态方法和非静态方法Java

时间:2011-12-24 17:19:52

标签: java

我知道我做了些蠢事,但我无法弄清楚如何修复它。

使用private方法时,问题出在removeVowels方法vowels内。

编译器提供

non-static variable vowels cannot be referenced from a static context

这是我的代码:

   public class RecursionHW2 {

            String vowels;

            // Part (A) First way
            public static int upperCase(String myString){

                return upperCaseChecker(myString , 0 );
            }

            public static int upperCaseChecker(String myString, int index){

                int inc;

                //My Base Code
                if(myString.length() <= index) return 0;
                if(Character.isUpperCase(myString.charAt(index)) == true) inc= 1;
                    else inc= 0;

                return inc+upperCaseChecker(myString,index+1);
            }



            // First way of Solving part (B)
            public static int count(String str, char a)
            {
                if (str.length() == 0)
                    return 0;
                else if (str.charAt(0) == a)
                    return 1 + count(str.substring(1, str.length()), a);
                else
                    return count(str.substring(1, str.length()), a);
            }


            //Second way of solving part (B)
            public static int anotherCount(String myString, char myWord)
            {
                return anotherCount(myString, myWord, 0);
            }

            public static int anotherCount(String myString, char myWord, int index)
            {
                int inc;

                if (index >= myString.length())
                {
                    return 0;
                }

                if (myString.charAt(index) == myWord)  inc =1;
                    else
                        inc = 0;

                return inc + anotherCount(myString, myWord, index+1);
            }



            // part (C) solving
            public Boolean isSorted(int[] a, int n)
            {
            if(n == 0 || n == 1) return true;
            else
            return isSorted(a, n, 1);
            }

            private Boolean isSorted(int[] a, int n, int cur)
            {
                if(cur == n) return true;

                if(a[cur - 1] <= a[cur])
                    return isSorted(a, n, cur+1);
                else
                    return false;
            }



            //part (D) Solving
            public static String removeVowels(String myString)
            {
                return removeVowels(myString, "");
            }

            private static String removeVowels(String myString, String t)
            {
                if(myString.length() == 0) return t;

                if(vowels.contains(myString.charAt(0) + ""))
                    return removeVowels(myString.substring(1), t);
                else
                    return removeVowels(myString.substring(1), t + myString.charAt(0));
            }


        public static void main(String[] args){


            //I've wrote 2 ways to solve the Second Recursive Q2
            System.out.println("Method 1: Number of Occurence " + count("Hello  This is Mohammad Fadin",'o'));
        //  System.out.println("Method 2: Number of Occurence "+ anotherCount("Hello This is Mohammad Fadin",'o'));

            String s1 = "Hello WorlDD";
            System.out.println("Number of Upper Cases " + upperCase(s1));

            String s2 = "Hello";
            System.out.println("After Vowels Removed " + removeVowels(s2));
        }


    }

8 个答案:

答案 0 :(得分:4)

您已使用static方法使用main“感染”了您的代码。在main方法中,你应该做这样的事情,这样你就不必做出所有事情static

public class RecursionHW2
{
  public static void main(String[] args)
  {
    RecursionHW2 rhw2 = new RecursionHW2();

    int count = rhw2.count("Hello world");

    // and so on
  }
}

答案 1 :(得分:3)

您无法从静态上下文引用实例变量。您必须先创建RecursionHW2的实例,或者使变量vowels 静态更有意义。或者您可以考虑从removeVowels方法中删除 static 修饰符。

<强>更新
但是,您的类看起来像一堆实用程序方法,因此您可能希望使其不可实例化(通过添加私有构造函数),使所有方法都是静态的(因为它们显然不在对象的状态下操作)并传递vowels作为removeVowels方法的附加参数。

答案 2 :(得分:1)

问题正是编译器告诉您的:您从静态上下文引用非静态(实例)变量vowels。实际上,几乎所有的方法都是静态的,这是一个非常糟糕的设计。

使所有需要访问实例数据的方法(此处:vowels实例变量)非静态,并在main()中实例化您的类。

答案 3 :(得分:0)

变化:

String vowels;

为:

static String vowels;

答案 4 :(得分:0)

您不能在静态方法中使用String元音,因为元音是非静态的。您需要在字符串中添加static关键字,然后您的代码才能正常工作。

答案 5 :(得分:0)

你可以使变量保持静态,或者只保持一切非静态,这将得到解决。你需要问自己的更大问题是我应该何时使用静态?何时不使用?

答案 6 :(得分:0)

更改

String vowels;

static String vowels;

您的所有方法都是静态的,因此不需要您的对象实例存在 - 即您不必说

x = new RecursionHW2(); 
x.upperCase(..);

但是,如果不使元音静态,除非实例化对象,否则它不存在。

答案 7 :(得分:0)

静态变量属于类,静态变量不属于类实例(对象)。

<强>测试

class Foo {

    private static String vowers;
    private String bar;

}


Foo a = new Foo ()

正在创建类Foo的新实例,每个实例都有自己的变量栏,但所有共享 vowers 变量,因为它属于该类。静态方法也是如此。

在静态方法(类方法)中,您不能引用非静态的变量。为什么会这样?

想象一下,从静态方法中引用一个非静态的变量

class Foo {
    private static String vowers;
    private String bar;

    public static void exampleMethod () {
       bar = "home";
    }

}

如果你这样做:

Foo a = new Foo () / / has a new bar
Foo b = new Foo () / / has a new bar

vowers是一个单一变量,属于不是实例的类

当你

Foo.exampleMethod()

如果实例a的变量或b的变量实例,该方法不知道变量 bar 。因此,您只能从静态方法

访问该类的静态变量
相关问题