Results 1 to 6 of 6
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Question Static Reference

    This code gives me an error on line 10, saying "Cannot make a static reference to the non-static method LoadFile() from the type FileDistribution". What should I do to fix this error?

    Java Code:
    class FileDistribution{
        ...
        public void LoadFile() throws FileNotFoundException, IOException{
            ...
        }
        ...    
    }
    
    static void Playing(){
    	FileDistribution.LoadFile();
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Static Reference

    Create an instance of the FileDistribution class and use its reference variable to call its methods.
    Or make loadFile() a static method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: Static Reference

    try putting the called code in a non statc method and call that method

  4. #4
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Static Reference

    Quote Originally Posted by Norm View Post
    Create an instance of the FileDistribution class and use its reference variable to call its methods.
    Or make loadFile() a static method.
    An instance...? :o

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Static Reference

    The new statement creates an instance of a class:
    TheClass refToTheClass = new TheClass(); // create an instance of TheClass
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Static Reference

    Quote Originally Posted by Zelaine View Post
    An instance...? :o
    Think of it like this. Book is a class. Harry Potter and the Philosophers Stone is an instance of a Book.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2013, 02:30 AM
  2. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  3. Garbage Collection - Self reference, static and non-static.
    By garyiskidding in forum Advanced Java
    Replies: 10
    Last Post: 03-17-2011, 11:22 AM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •