Results 1 to 8 of 8
  1. #1
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default static reference to non static method

    Trying to fill out some basic code for a homework assignment in my CS class. I am attempting to use a method to construct an arraylist, but when I am getting an error when I try to call on the method from the main program. The errors I am getting are on lines 13-17. Here is a short bit of my code:

    Java Code:
    import java.util.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    public class MovieQueueMain {
    	
    	/* You may create additional private static 
    	   variables or methods as needed */
    
    	public static void main(String args[]) {
    		
    		LinkedList<String> movieQueue = new LinkedList<String>();
    		LinkedList<String> comedy = getList("comedy");
    		LinkedList<String> drama = getList("drama");
    		LinkedList<String> action = getList("action");
    		LinkedList<String> horror = getList("horror");
    		LinkedList<String> family = getList("family");
    		
    	}
    	
    	public LinkedList<String> getList(String listName){
    	
    		try{
    	    	
    	    	LinkedList<String> temp = new LinkedList<String>();
    	    	int i = 0;
    	        String fileName = "//Users//zerosdamnation//Documents//workspace//CS367//bin//Project2//" + listName + ".txt";
    	        File srcFile = new File(fileName);
    	        Scanner fileIn = new Scanner(srcFile);
    	         // for reading from the file
    	        
    	       
    	       
    	        while (fileIn.hasNextLine()){
    	            String inLine = fileIn.nextLine();
    	            temp.add(inLine);	                
    	            i++;
    	           
    	        }
    	        return temp;
    		}
    	    catch (FileNotFoundException e){
    	        System.out.println("No Such File");
    	    }
    		return null;
    	}	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: static reference to non static method

    Move all the code out of the main() method into the class's constructor and add a call to the constructor to the main() method. The getList() method only exists to be called when there is an instance of the MovieQueueMain class.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: static reference to non static method

    Thanks!

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: static reference to non static method

    Another way is to create an instance of MovieQueueMain class. And you'll be able to access the getList() method using this instance like:

    Java Code:
    MovieQueueMain mq = new MovieQueueMain();
    LinkedList<String> comedy = mq.getList("comedy");

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: static reference to non static method

    you call non-static method from static method like..

    Main method is is a static method public static void main(String args[])

    public LinkedList<String> getList(String listName) this method is non-static one.

    So java not support static method call non-static method.
    you create this public LinkedList<String> getList(String listName) into static one like below

    Java Code:
    public static LinkedList<String> getList(String listName){
    //code here...
    }
    now it's work
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: static reference to non static method

    Quote Originally Posted by tamilarasi View Post
    you call non-static method from static method like..

    Main method is is a static method public static void main(String args[])

    public LinkedList<String> getList(String listName) this method is non-static one.

    So java not support static method call non-static method.
    you create this public LinkedList<String> getList(String listName) into static one like below

    Java Code:
    public static LinkedList<String> getList(String listName){
    //code here...
    }
    now it's work
    Sorry, but that is the worst possible suggestion! Static methods are evil with very few exceptions.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: static reference to non static method

    Quote Originally Posted by PhHein View Post
    Sorry, but that is the worst possible suggestion! Static methods are evil with very few exceptions.
    can you explain clearly. I couldn't get it..
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: static reference to non static method

    There are many posts, articles and discussions about static methods. Among those are these:
    java - Why are static variables considered evil? - Stack Overflow
    Static methods in Java are evil, mostly | Sneaking suspicion
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. 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
  2. Garbage Collection - Self reference, static and non-static.
    By garyiskidding in forum Advanced Java
    Replies: 10
    Last Post: 03-17-2011, 11:22 AM
  3. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  4. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  5. Replies: 3
    Last Post: 02-09-2010, 05:22 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
  •