non static variable cannot be referenced from a static context

前端 未结 5 970
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 05:31

I am a Java newbie, so please bear with me and help.

I aim to first play then record sound.

I use netbeans IDE 6.8. Here is the code:

import java         


        
相关标签:
5条回答
  • 2021-01-29 06:02

    Static means that there is no object (instance of class, like new YourClass()). If you have a static function, it is supposed to be "standalone", so it cannot use non-static things, because they use stuff in a real object (like member variables etc.)

    Any static function can only call and use other static functions. So look for the trouble in that environment.

    0 讨论(0)
  • 2021-01-29 06:05

    Replace your main with :

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Reg1 r = new Reg1();
                try {
                    AudioInputStream audio = AudioSystem.getAudioInputStream(new File("name.wav"));
                    Clip clip = AudioSystem.getClip();
                    clip.open(audio);
                    clip.start();
                }
                catch (Exception e) {
                    System.out.println(e);
                }
                r.captureAudio();
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-29 06:05

    The captureAudio() is not declared static thus it is an instance method and it cannot be called without an object in main() which is static.

    Sine I see you have createed in the run methid new instance of Reg1 thus it is fine then to call the method captureAudio() for this object. Plus there is no need for the inner private void Reg1() method which BTW you were never calling. I propose that you change main to something like this.

        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    Reg1 reg1 = new Reg1();
                    KeyListener s;    
                    try
                    {
                        AudioInputStream audio = AudioSystem.getAudioInputStream(new File("name.wav"));
                        Clip clip = AudioSystem.getClip();
                        clip.open(audio);
                        clip.start();
                    }catch(UnsupportedAudioFileException uae)
                    {
                        System.out.println(uae);
                    }catch(IOException ioe)
                    {
                        System.out.println(ioe);
                    }catch(LineUnavailableException lua)
                    {
                        System.out.println(lua);
                    }
                    reg1.captureAudio();
                }
            });
        }
    
    0 讨论(0)
  • 2021-01-29 06:14

    While there may be more formal resources to explain this (e.g. the Java Language Specification), I frequently find myself checking this webpage for these kinds of questions:

    http://mindprod.com/jgloss/jgloss.html

    It explains these things quite well, also for noobs ;-) In your case:

    • http://mindprod.com/jgloss/static.html also linking to:
    • http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html
    0 讨论(0)
  • 2021-01-29 06:19

    I used the "Java Editor" and I tried to compiled your class. I got the error masseage

    Reg1.java:93:16: non-static method captureAudio() cannot be referenced from a static context

    The reason is: you call in the method static void main() a method, which need an instance of a object. If a method is not declared as static you need follwoing syntax 'referenceObject.captureAudio()'

    0 讨论(0)
提交回复
热议问题