Java:Calling a method from class

Thread Starter

justin2014

Joined Nov 26, 2014
24
I would like to know whether we can call a method from class rather than from static main method.While executing this code I've got error stating:

D:\Downloads\PRO>javac start.java
start.java:9: error: illegal start of type
this.method();
^
start.java:9: error: ';' expected
this.method();
^
start.java:9: error: invalid method declaration; return type required
this.method();
^
3 errors.
Code:
import java.io.*;
class start
{
public static void main(String args[])
{
System.out.println("STARTING JAVA PROGRAM");
}
finish f=new finish();
f.method();
}
class finish
{
public void method()
{
System.out.println("FINISHING JAVA PROGRAM");
}
}
Also I would like to know when a compiler starts whether it invokes the static main method or whether it invokes the class which contains the static main method.Could anyone help me.
 

sirch2

Joined Jan 21, 2013
1,037
To answer your question yes you can call a public method on an object of a class from any other class. I suspect you shouldn't call your methods"method" that might fix the errors, I can't see what else is causing the problem, assuming the code you posted is the code you are trying to compile.

The compiler does not invoke anything, it "compiles" your source code into byte code. You then run the code with the "java" command and you tell the java command which class has the main method.

In Java class names normally start with an upper case letter by convention e,g, Start and Finish and public classes must be in separate files with the same name as your class, e.g. Start.java and Finish.java. I think your code will work since you have not specified access so they will default to private. I suggest you follow one of the many Java tutorials and use some more meaningful names for classes and methods.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
To answer your question yes you can call a public method on an object of a class from any other class. I suspect you shouldn't call your methods"method" that might fix the errors, I can't see what else is causing the problem, assuming the code you posted is the code you are trying to compile.

The compiler does not invoke anything, it "compiles" your source code into byte code. You then run the code with the "java" command and you tell the java command which class has the main method.

In Java class names normally start with an upper case letter by convention e,g, Start and Finish and public classes must be in separate files with the same name as your class, e.g. Start.java and Finish.java. I think your code will work since you have not specified access so they will default to private. I suggest you follow one of the many Java tutorials and use some more meaningful names for classes and methods.
Thanks.But whether is it possible to display "FINISHING JAVA PROGRAM" without calling the method:method()?Also could you tell me how to display both "STARTING JAVA PROGRAM" and "FINISHING JAVA PROGRAM" where I would like to have:
  1. object creation of Class Finish and method call outside static main method in Class Start
  2. method should be in Class Finish in the non static block
Could you help me.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
To answer your question yes you can call a public method on an object of a class from any other class. I suspect you shouldn't call your methods"method" that might fix the errors, I can't see what else is causing the problem, assuming the code you posted is the code you are trying to compile.

The compiler does not invoke anything, it "compiles" your source code into byte code. You then run the code with the "java" command and you tell the java command which class has the main method.

In Java class names normally start with an upper case letter by convention e,g, Start and Finish and public classes must be in separate files with the same name as your class, e.g. Start.java and Finish.java. I think your code will work since you have not specified access so they will default to private. I suggest you follow one of the many Java tutorials and use some more meaningful names for classes and methods.
Sorry for asking that question.I accidentally named the java file as temp.java instead of naming it as start.java.It seems that I can't update the post or else I would have changed the class names to proper case.
 

sirch2

Joined Jan 21, 2013
1,037
I am not entirely sure what you are trying to achieve, as I said you probably need to work through some tutorials, but something like this?

Starter.java
Code:
package demo;

public class Starter {

    public static void main(String[] args) {

        Starter s = new Starter();
        s.start();

    }
   
   
    protected void start() {
        System.out.println("STARTING JAVA PROGRAM");
       
        Finisher f= new Finisher();
        f.finish();
    }

}

Finisher.java
Code:
package demo;

public class Finisher {

    public void finish() {
        System.out.println("FINISHING JAVA PROGRAM");
    }
}
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
I am not entirely sure what you are trying to achieve, as I said you probably need to work through some tutorials, but something like this?

Starter.java
Code:
package demo;

public class Starter {

    public static void main(String[] args) {

        Starter s = new Starter();
        s.start();

    }
  
  
    protected void start() {
        System.out.println("STARTING JAVA PROGRAM");
      
        Finisher f= new Finisher();
        f.finish();
    }

}

Finisher.java
Code:
package demo;

public class Finisher {

    public void finish() {
        System.out.println("FINISHING JAVA PROGRAM");
    }
}
Sorry for asking that question.The code was wrong since you can't call a method outside an internal method but now it's working.But could you just tell is this statement correct?
Statement:"Its never possible to call a method within a non-static block for the first written method.
Reason I suggest is:If you try to start defining a method within an non static block then for each method you have to find another method and it goes on recursively isn't it?
Could you tell your opinion.
 

sirch2

Joined Jan 21, 2013
1,037
You are correct but it is even more specific, the only way a Java program will run is if there is a method called "main" that has public static access and void return with the arguments String[] main - i.e.

public static void main(String[] args)

Any other static method signature will not work as an entry point. You can have as many other static methods as you like but only that one will be recognized as the entry point. This pretty much comes from C/C++ which have similar entry points. There must be some way to know where to start executing code and the "main" method is it.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
You are correct but it is even more specific, the only way a Java program will run is if there is a method called "main" that has public static access and void return with the arguments String[] main - i.e.

public static void main(String[] args)

Any other static method signature will not work as an entry point. You can have as many other static methods as you like but only that one will be recognized as the entry point. This pretty much comes from C/C++ which have similar entry points. There must be some way to know where to start executing code and the "main" method is it.
Okay.Could you tell what's wrong in this code:
Code:
import java.io.*;
class temp
{
public static void main(String args[])
{
Finish f=new Finish();
System.out.println("STARTING JAVA PROGRAM");
ignite();
}
static void ignite()
{
f.end();
}
}
class Finish
{
void end()
{
System.out.println("FINISHING JAVA PROGRAM");
}
}
 
Last edited:

sirch2

Joined Jan 21, 2013
1,037
What is this, some kind of test of my Java skills (which btw, is on-and-ff professionally/commercially since the Beta came out in '96) ?

You tell me what is wrong with it, I will help if you are stuck. STOP CALLING YOUR METHODS "method" it is at best meaningless.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
What is this, some kind of test of my Java skills (which btw, is on-and-ff professionally/commercially since the Beta came out in '96) ?


You tell me what is wrong with it, I will help if you are stuck. STOP CALLING YOUR METHODS "method" it is at best meaningless.
Yeah I have updated the post by changing method() to end().I'm stuck with these errors that compiler says:
  1. temp.java.9:error: illegal start of expression
  2. temp.java.9:error: ';' expected
Could you help me in rectifying these errors.Should we remove any method from static block or add any method to the static block?
 

kubeek

Joined Sep 20, 2005
5,795
Most likely you cannot define a function within a function (a.k.a method for those who like these obscure names for well established things)
 

sirch2

Joined Jan 21, 2013
1,037
"main" is not a static block it's a static method and it cannot contain other methods (except anonymous ones but leave that for now). You cannot put "void ignite()" inside the main method.

Static blocks look like this

Code:
static ArrayList<String> messages;

static {
//initalise some static variables in this static block
messages.add("Hello");
messages.add( "goodbye");
}
A static block cannot contain methods an it is initialized as soon as the class is loaded by the class loader and only executed once. For what you are doing you do not need a static block.
 

sirch2

Joined Jan 21, 2013
1,037
BTW what language do you normally program in? I'm guessing you are trying to translate the structure of some other language into an object oriented one and it just will not fit, square peg, abstract object shaped hole.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
Most likely you cannot define a function within a function (a.k.a method for those who like these obscure names for well established things)
Thanks.Could you check whether this code is written as you said:
Code:
import java.io.*;
class temp
{
public static void main(String args[])
{
Finish f=new Finish();
System.out.println("STARTING JAVA PROGRAM");
ignite();
}
static void ignite()
{
f.end();
}
}
class Finish
{
void end()
{
System.out.println("FINISHING JAVA PROGRAM");
}
}
 
Last edited:

Thread Starter

justin2014

Joined Nov 26, 2014
24
"main" is not a static block it's a static method and it cannot contain other methods (except anonymous ones but leave that for now). You cannot put "void ignite()" inside the main method.

Static blocks look like this

Code:
static ArrayList<String> messages;

static {
//initalise some static variables in this static block
messages.add("Hello");
messages.add( "goodbye");
}
A static block cannot contain methods an it is initialized as soon as the class is loaded by the class loader and only executed once. For what you are doing you do not need a static block.
Can I call a method from static main.Could you look at this code too.After compiling this I'm getting an error:

D:\Downloads\PRO>javac temp.java
temp.java:12: error: cannot find symbol
f.end();
^
symbol: variable f
location: class temp
1 error
The code is:

Code:
import java.io.*;
class temp
{
public static void main(String args[])
{
Finish f=new Finish();
System.out.println("STARTING JAVA PROGRAM");
ignite();
}
static void ignite()
{
f.end();
}
}
class Finish
{
void end()
{
System.out.println("FINISHING JAVA PROGRAM");
}
}
 

kubeek

Joined Sep 20, 2005
5,795
Please use indentation and code tags, this is completely illegible. Disregard my previous comment, I thought the braces were in different places.
Code:
import java.io.*;
class temp
{
    public static void main(String args[])
    {
    Finish f=new Finish();
    System.out.println("STARTING JAVA PROGRAM");
    ignite();
    }
  
    static void ignite()
    {
    f.end();
    }
}

class Finish
{
    void end()
    {
    System.out.println("FINISHING JAVA PROGRAM");
    }
}
You class finish is missing a constructor. F is not a global variable in class temp so function ignite cannot use that - either make it global or pass it as a parameter.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
Please use indentation and code tags, this is completely illegible. Disregard my previous comment, I thought the braces were in different places.
Code:
import java.io.*;
class temp
{
    public static void main(String args[])
    {
    Finish f=new Finish();
    System.out.println("STARTING JAVA PROGRAM");
    ignite();
    }
 
    static void ignite()
    {
    f.end();
    }
}

class Finish
{
    void end()
    {
    System.out.println("FINISHING JAVA PROGRAM");
    }
}
You class finish is missing a constructor. F is not a global variable in class temp so function ignite cannot use that - either make it global or pass it as a parameter.
Thanks I got the output.But could you tell me these doubts:
  1. why do we need a constructor for class finish?
  2. why do we need to pass it as a parameter and how can we pass it?
 

kubeek

Joined Sep 20, 2005
5,795
Because you are calling new finish() what do you expect to happen? The contructor can be empty, but has to exist.
For example like this:
Code:
import java.io.*;
class temp
{
   Finish f;
    
    public static void main(String args[])
    {
    f=new Finish();
    System.out.println("STARTING JAVA PROGRAM");
    ignite();
    }
    static void ignite()
    {
    f.end();
    }
}

class Finish
{
    finish()
   { 
    }

    void end()
    {
    System.out.println("FINISHING JAVA PROGRAM");
    }
}
 

sirch2

Joined Jan 21, 2013
1,037
or you could instantiate a Finish object in the ignite method and then call end -

Code:
static void ignite() {
    Finish f = new Finish();
    f.end();
}
or even

Code:
static void ignite() {
    new Finish().end();  //I think this is ok
}
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
or you could instantiate a Finish object in the ignite method and then call end -

Code:
static void ignite() {
    Finish f = new Finish();
    f.end();
}
or even

Code:
static void ignite() {
    new Finish().end();  //I think this is ok
}
Could you tell whether we can define methods or call methods in constructors.I'm asking whether we can extend constructor definition to methods or classes.I would like to know whether we can write a program in java like this:
Code:
import java.io.*;
class temp
{
  temp()
 {
Finish f=new Finish();
f.end();
 }
  public static void main(String args[])
 {
 System.out.println("STARTING JAVA PROGRAM");
 }
 }
class Finish
{
  public void end()
 {
 System.out.println("ENDING JAVA PROGRAM");
 }
}
I'm getting a partial output but I can't display "ENDING JAVA PROGRAM.I tried to indent the program.Could you tell the basic rules to indent a program.Could you help me.
 
Top