Reputation: 189
First off I want to say that I'm very new to Java, coming from a C++ background. I can never get in contact with my teacher so I would like to try to post this question here that I have been wondering for a while (hopefully I can word it correctly):
How do I create methods without using static
? I know I probably need to make a class for it but how do I go about it? Just a class without variables and just functions? Do I make a second class that encompasses main other than the class named after the .java file? For instance:
package musiclibrary;
import java.util.Scanner;
/**
* This class implements a user to create a playlist from a selection of artists and songs
* @author ahb5190
*/
public class MusicLibrary {
static String divider = "*****************************************************";
//Scanner class
static Scanner input = new Scanner(System.in);
/**
* Welcome menu
*/
public static void welcomeMenu()
{
System.out.println(divider);
System.out.println();
System.out.println("Welcome to Art's personal music library!");
System.out.println();
System.out.println("Choose an option:");
System.out.println("1) Create Playlist");
System.out.println("2) Delete Playlist");
System.out.println("3) Add Selection to Playlist");
System.out.println("4) Remove Selection from Playlist");
System.out.println("5) Quit");
System.out.println();
System.out.print("Your choice?: ");
}
/**
*
* @param min error check low bound
* @param max error check high bound
* @return
*/
public static int getData(int min, int max)
{
boolean goodInput = false;
int choice = 1; //Will be overwritten
while(!goodInput)
{
choice = input.nextInt();
if(choice < min || choice > max)
{
System.out.print(choice + " is not a valid choice. Please enter a number between " + min + " and " + max + ": ");
goodInput = false;
}
else
{
goodInput = true;
}
}
return choice;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Variables
int getDataMin = 1;
int getDataMax = 5;
boolean quit = false;
int userInput;
do {
welcomeMenu();
userInput = getData(getDataMin, getDataMax);
if (userInput == 1)
{
quit = false;
}
else if (userInput == 2)
{
quit = false;
}
else if (userInput == 3)
{
quit = false;
}
else if (userInput == 4)
{
quit = false;
}
else if (userInput == 5)
{
quit = true;
}
} while(!quit);
}
}
is the very beginning of a java program that was assigned. If I remove static
from public static void welcomeMenu()
it gives me non-static method welcomeMenu() cannot be referenced from a static context
when I try to call welcomeMenu();
in main.
Another block of code (not very neat, was part of a timed exam):
package lalala;
/**
*
* @author ahb5190
*/
public class Lalala {
public class Movie
{
private String title;
private String genre;
private String director;
private String star;
public Movie (String t, String g, String d, String s)
{
title = t;
genre = g;
director = d;
star = s;
}
public String gettitle()
{
return title;
}
public String getGenre()
{
return genre;
}
public String getDirector()
{
return director;
}
public String getStar()
{
return star;
}
public void setTitle(String x)
{
title = x;
}
public void setGenre(String x)
{
genre = x;
}
public void setDirector(String x)
{
director = x;
}
public void setsStar(String x)
{
star = x;
}
public boolean equals(Movie otherMovie)
{
if(otherMovie == null)
{
return false;
}
else
{
return title.equals(otherMovie.title) && genre.equals(otherMovie.genre) && director.equals(otherMovie.director) && star.equals(otherMovie.star);
}
}
@Override
public String toString()
{
return(title + " " + genre + " " + director + " " + star);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
Movie a;
a = new Movie("Star Trek into Darkness", "Sci-fi", "J.J. Abrams", "Chris Pine"); //error: non-static variables this cannot be referenced from a static context
Movie b = new Movie("Star Trek", "Sci-Fi", "J.J. Abrams", "Chris Pine"); //error: non-static variables this cannot be referenced from a static context
Movie c = new Movie("Independence Day", "Action", "Roland Emmerich", "Will Smith"); //error: non-static variables this cannot be referenced from a static context
System.out.println("Movies");
System.out.println("Title: " + a.title);
System.out.println("Genre: " + a.genre);
System.out.println("Director: " + a.director);
System.out.println("Star: " + a.star);
System.out.println();
System.out.println("Title: " + b.title);
System.out.println("Genre: " + b.genre);
System.out.println("Director: " + b.director);
System.out.println("Star: " + b.star);
System.out.println();
System.out.println("Title: " + c.title);
System.out.println("Genre: " + c.genre);
System.out.println("Director: " + c.director);
System.out.println("Star: " + c.star);
System.out.println();
a.equals(b);
}
}
gives me same static variable error as before, as commented on the code above. In that scenario, how I got it to 'work' was to remove static
from public static void main(String args[])
.
Really trying to learn the right way to Java, any help would be appreciated. Also I hope this meets MCV.
Upvotes: 0
Views: 89
Reputation: 530
First of all non-static methods can be call only with instance of the class,
This link might help, to understand it a bit.And this type of question is already been answered here: this
Upvotes: 0
Reputation: 2155
To access non static members of a class you need an instance of that class.
So new Movie("a", "b", "c", "d").getGenre()
is legal.
Removing static keyword from main is not legal since it is the entry point of your program and thus it is mandatory to exists.
EDIT: In the first source, the main() method does not create any instance of MusicLibrary, that's why you need to use static on all members.
Add MusicLibrary lib = new MusicLibrary();
then call lib.welcomeMenu();
and you can get rid of static keyword.
Upvotes: 1