Java:Calling a method from class

Discussion in 'Programmer's Corner' started by justin2014, Dec 1, 2014.

  1. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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 (Text):
    1. import java.io.*;
    2. class start
    3. {
    4. public static void main(String args[])
    5. {
    6. System.out.println("STARTING JAVA PROGRAM");
    7. }
    8. finish f=new finish();
    9. f.method();
    10. }
    11. class finish
    12. {
    13. public void method()
    14. {
    15. System.out.println("FINISHING JAVA PROGRAM");
    16. }
    17. }
    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.
     
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  3. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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.
     
  4. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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.
     
  5. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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 (Text):
    1. package demo;
    2.  
    3. public class Starter {
    4.  
    5.     public static void main(String[] args) {
    6.  
    7.         Starter s = new Starter();
    8.         s.start();
    9.  
    10.     }
    11.    
    12.    
    13.     protected void start() {
    14.         System.out.println("STARTING JAVA PROGRAM");
    15.        
    16.         Finisher f= new Finisher();
    17.         f.finish();
    18.     }
    19.  
    20. }
    21.  

    Finisher.java
    Code (Text):
    1. package demo;
    2.  
    3. public class Finisher {
    4.  
    5.     public void finish() {
    6.         System.out.println("FINISHING JAVA PROGRAM");
    7.     }
    8. }
    9.  
     
  6. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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.
     
  7. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  8. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    Okay.Could you tell what's wrong in this code:
    Code (Text):
    1. import java.io.*;
    2. class temp
    3. {
    4. public static void main(String args[])
    5. {
    6. Finish f=new Finish();
    7. System.out.println("STARTING JAVA PROGRAM");
    8. ignite();
    9. }
    10. static void ignite()
    11. {
    12. f.end();
    13. }
    14. }
    15. class Finish
    16. {
    17. void end()
    18. {
    19. System.out.println("FINISHING JAVA PROGRAM");
    20. }
    21. }
     
    Last edited: Dec 2, 2014
  9. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  10. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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?
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    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)
     
  12. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    "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 (Text):
    1.  
    2. static ArrayList<String> messages;
    3.  
    4. static {
    5. //initalise some static variables in this static block
    6. messages.add("Hello");
    7. messages.add( "goodbye");
    8. }
    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.
     
  13. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  14. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    Thanks.Could you check whether this code is written as you said:
    Code (Text):
    1. import java.io.*;
    2. class temp
    3. {
    4. public static void main(String args[])
    5. {
    6. Finish f=new Finish();
    7. System.out.println("STARTING JAVA PROGRAM");
    8. ignite();
    9. }
    10. static void ignite()
    11. {
    12. f.end();
    13. }
    14. }
    15. class Finish
    16. {
    17. void end()
    18. {
    19. System.out.println("FINISHING JAVA PROGRAM");
    20. }
    21. }
     
    Last edited: Dec 2, 2014
  15. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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 (Text):
    1. import java.io.*;
    2. class temp
    3. {
    4. public static void main(String args[])
    5. {
    6. Finish f=new Finish();
    7. System.out.println("STARTING JAVA PROGRAM");
    8. ignite();
    9. }
    10. static void ignite()
    11. {
    12. f.end();
    13. }
    14. }
    15. class Finish
    16. {
    17. void end()
    18. {
    19. System.out.println("FINISHING JAVA PROGRAM");
    20. }
    21. }
     
  16. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Please use indentation and code tags, this is completely illegible. Disregard my previous comment, I thought the braces were in different places.
    Code (Text):
    1. import java.io.*;
    2. class temp
    3. {
    4.     public static void main(String args[])
    5.     {
    6.     Finish f=new Finish();
    7.     System.out.println("STARTING JAVA PROGRAM");
    8.     ignite();
    9.     }
    10.  
    11.     static void ignite()
    12.     {
    13.     f.end();
    14.     }
    15. }
    16.  
    17. class Finish
    18. {
    19.     void end()
    20.     {
    21.     System.out.println("FINISHING JAVA PROGRAM");
    22.     }
    23. }
    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.
     
  17. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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?
     
  18. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    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 (Text):
    1. import java.io.*;
    2. class temp
    3. {
    4.    Finish f;
    5.    
    6.     public static void main(String args[])
    7.     {
    8.     f=new Finish();
    9.     System.out.println("STARTING JAVA PROGRAM");
    10.     ignite();
    11.     }
    12.     static void ignite()
    13.     {
    14.     f.end();
    15.     }
    16. }
    17.  
    18. class Finish
    19. {
    20.     finish()
    21.    {
    22.     }
    23.  
    24.     void end()
    25.     {
    26.     System.out.println("FINISHING JAVA PROGRAM");
    27.     }
    28. }
     
  19. sirch2

    Well-Known Member

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

    Code (Text):
    1. static void ignite() {
    2.     Finish f = new Finish();
    3.     f.end();
    4. }
    or even

    Code (Text):
    1. static void ignite() {
    2.     new Finish().end();  //I think this is ok
    3. }
     
  20. justin2014

    Thread Starter New Member

    Nov 26, 2014
    24
    0
    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 (Text):
    1. import java.io.*;
    2. class temp
    3. {
    4.   temp()
    5.  {
    6. Finish f=new Finish();
    7. f.end();
    8.  }
    9.   public static void main(String args[])
    10.  {
    11.  System.out.println("STARTING JAVA PROGRAM");
    12.  }
    13.  }
    14. class Finish
    15. {
    16.   public void end()
    17.  {
    18.  System.out.println("ENDING JAVA PROGRAM");
    19.  }
    20. }
    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.
     
Loading...