java: calling a method - java

I am just learning how to create a subroutine/method in java, and I am having the problem that I can't call my method with the compiler thinking that my call (playGame();) is an attempted definition of a method of itself. So i get the error "invalid method declaration; return type required". As I am a beginner I am sure that it is a stupid mistake, but I have tried rewriting many times to fix it and I cannot figure it out.
public class GUI {
public static void main(String[] args){
}
public static void playGame() {
}
playGame();
}

You can only call a method from within another method, not from the body of a class. Move the line
playGame();
inside the main method:
public static void main(String[] args){
playgame();
}

Your method call should be inside another method , in this case main (or) You can call from playGame() also, but that would be recursion and may end up in infinite loop.
public static void main(String[] args){
playGame();
}
See Essentials of the Java Programming Language to learn more about how to write java program.

You can not call methods from class body directly the way you are doing. You need to call method playGame() from main method. Like :
public class GUI {
public static void main(String[] args){
playGame();
}
public static void playGame() {
// some statements
}
}
As you are new Start Reading Java Tutorial.

Related

Unable to call function in java

When I try to call the lyrics function in this code, "invalid method declaration; return type required" occurs.
I'm learning java and very very new to it. I'm confused how to define the function and call the function so that code may run.
public class Main {
public static void main(String[] args) {
}
public void lyrics() {
System.out.println("some lyrics here");
}
lyrics();
}
Normally, one can't just invoke a method randomly in the body of the code. However, there is something called an initialization block (this gets run in the body of the constructors of the object). I think an example might clarify. Like,
public class Main {
public static void main(String[] args) {
new Main(); // <-- instantiate an instance of Main
}
public void lyrics() {
System.out.println("some lyrics here");
}
{ // <-- this is an initialization block
lyrics();
}
}
The above uses the default constructor, we can add an explicit one. Like,
public Main() {
super();
System.out.println("In Main constructor");
}
Note how the output changes.
They can also be static (and run when the class is first referenced). Like,
public class Main {
public static void main(String[] args) {
}
public static void lyrics() {
System.out.println("some lyrics here");
}
static {
lyrics();
}
}
Your code is nearly correct. Your lyrics() method must be static if you want to call it inside main method because main method is static. Non static members cannot be accessed from static method (without creating an instance to invoke it).
public class Main {
public static void main(String[] args) {
lyrics();
}
public static void lyrics() {
System.out.println("some lyrics here");
}
}
You can invoke non-static methods from static method by creating an instance of the class containing non-static method inside your main method as mentioned in comments by Elliott Frisch.
new Main().lyrics();
Since you're learning Java, you must remember that the only way to call a non-static method from a static method is to have an instance of the class containing the non-static method.
Or you can simply make your function static. Also, you need to call the function inside the main block.
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
Happy coding!
The method should be declared as static and function call should be within main method. The correct code should look like as follows:
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
You need to move the call to lyrics() into the main method where code is actually executed. Where you have it now, the compiler is expecting a new method defination.
public class Main {
public static void main(String[] args) {
// execution begins here.
new Main().lyrics();
}
public void lyrics() {
System.out.println("some lyrics here");
}
}
EDIT: created new class Main class to avoid errors.

Why can an instance only be seen by main methods

public static void main(String[] args)
{
GUI TestGUI = new GUI();
TestGUI.setVisible(true);
}
public void blahh()
{
TestGUI.setVisible(true);
}
Can not find symbol for TestGUI in blahh, but can be seen in the main method.
How can I access TestGUI from other methods?
This is a scope issue. You could solve this by passing your GUI object to the blahh() method. Currently your blahh method has no way of reaching that variable.
public void blahh(GUI testGui) {
...
}
You can then call this method like this:
blahh(testGui);
Here is some reading you can do on scope, hopefully it will be helpful
Alternatively, you could declare your testGui variable as a field, and it will be accessible from anywhere in the class (make sure to make it static if you must access it in a static method). However, this will offer you less privacy with that variable even though it might seem more convenient.
because you declared TestGUI inside main method as a local varaible to a method , declare it as a class property
static GUI TestGUI;
public static void main(String[] args)
{
Test = new GUI();
TestGUI.setVisible(true);
}
public void blahh()
{
TestGUI.setVisible(true);
}

In java constructor and main which one will execute first?

Basically which one will execute first the main method or the constructor?
public class ConstructorExp {
public ConstructorExp() {
System.out.println("Ctt");
}
public static void main(String[] args) {
System.out.println("Inside Main Methos");
System.out.println("Main");
}
}
The main method will always be excecuted first, because it is a special static method that will be called from Java itself to start an application.
For more about the main method please read Java main() Method Explained for example.
Constructors will be created on object creation - in your case no object creation happens - so the constructor will never be executed.
You could modify your example to also execute the constructor:
public class ConstructorExp {
public ConstructorExp() {
System.out.println("Ctt");
}
public static void main(String[] args) {
System.out.println("Inside Main Methos");
ConstructorExp example = new ConstructorExp();
System.out.println("Main");
}
}
Be carefully, because the example object is never used the constructor call might be eliminated by some kind of optimization depending on the compiler you are using.

Importing a Java custom class into another program

I am a first year comp sci student and going through my first round of textbook problems, all dealing with the System.out.println method (no, im not looking for help on homework problems. i've satisfied (as far as i know right now) the requirements of the problem, am just looking to gain some extra information).
The first problem asked me to write a program that would output this:
//////////////////////
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
This was no problem. I wrote the following code:`
public class Stewie {
public static void main(String[] args) {
line();
qoute();
line2();
}
public static void line() {
System.out.println("//////////////////////");
}
public static void qoute() {
System.out.println("|| Victory is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}`
Later in the text, it asks me to write a program that prints the above figure 5 times in a row. This is also not a problem, I just rewrote the code from the first problem, like this:
/*
*/
public class Stewie2 {
public static void main(String[] args){
newStewie();
newStewie();
newStewie();
newStewie();
newStewie();
}
public static void newStewie() {
line();
qoute();
line2();
}
public static void line(){
System.out.println("//////////////////////");
}
public static void qoute(){
System.out.println("|| Victory is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}
That's all good and well, but I'd like to know how to import class Stewie from the first problem so I could use that in the second one without having to rewrite all the code. Can anyone help me out??
edit:re:importing a custom java class. I saw this before posting, but probably do not know enough about programming for it to be helpful to me at the moment. thank you though.
Have a look on how to create and use packages.
Because your methods in your first class are static declared, you can just call them within your second class.
package mypackage;
public class Stewie
{
public static void main(String[] args)
{
line();
qoute();
line2();
}
public static void line()
{
System.out.println("//////////////////////");
}
public static void qoute()
{
System.out.println("|| Victory is mine! ||");
}
public static void line2()
{
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}
The seconds class should also be in the same package. Then, because your methods declared in the first class are static, you can directly call them to your second class.
package mypackage;
public class Stewie2
{
public static void main(String[] args)
{
Stewie.line();
Stewie.qoute();
Stewie.line2();
}
}
Now in your second class, you can extend your code with further functionality.
Note: As already mentioned, this is allowed because your methods are static.
If your method, bellow wasn't static:
public void qoute()
{
System.out.println("|| Victory is mine! ||");
}
You would get the following error message:
Non-static method 'qoute()' cannot be referenced from a static context.
So be-careful and notice the difference.

Which is the first method called when Java executes a program?

I am learning core Java and I have one question, "Which is the first method called when the program is executed?"
That would be the main method.
It should be declared as
public static void main(String[] args)
It needs to be public, since the JVM should have access to call the method.
It needs to be static, as no objects are instantiated when the program starts
It takes an array of Strings as argument (coming from the command-line)
Some good links to have a look at:
The main Method (from the official Getting started trail)
What is the main method
Entry point for Java applications: main(), init(), or run()?
Some people may recommend you to write
public static void main(String... args)
this is equivalent to String[] args if you're using a compiler of version 1.5 or later. (I would discourage this unless you're extensively calling your main method internally with a varying number of arguments.)
Others may suggest
public static void main(String args[])
This is also equivalent, but discouraged by the Java Coding Convention.
It's usually main. But in this program, it's pain:
public class WhatThe {
public static final int x = pain();
public static int pain() {
System.out.println("pain!");
return 0;
}
public static void main(String[] args) {
System.out.println("main");
}
}
As it is in this one:
public class WhatThe {
static {
pain();
}
public static void pain() {
System.out.println("pain!");
}
public static void main(String[] args) {
System.out.println("main");
}
}
This is unlikely to be useful knowledge, but it's something to be aware of.
public static void main(String ar[])
Java programs start executing at the main method, which has the following method heading:
public static void main(String[] args)
public static void main(String... args) //java 1.5+
public static void main(String args[])
Read more at docs
In addition to aioobes answer
A usual way to start a simple java program is to execute java like this:
java com.example.MyClass
com.example.MyClass (or your fully qualified class name) needs to have a main method with exactly this signature:
public static void main(String[] args)
(you're only allowed to change the name of the parameter, like arguments instead of args). The virtual machine will try to load the named class and try to invoke this static method which will "start the Java program".
The main method will be called first,control goes to main method first which has the method headings:
public static void main(String[] args) or
public static void main(String args[])

Categories