• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to invoke a method outside the main class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey folks,
I'm new to Java and programming in general, I've been experimenting a bit with Netbeans IDE and came upon some problem.
So I have 2 classes in this project- the main class (with the main method) and a second class (no main method).
For some reason, I am able to invoke a simple method at the main class, instantiating and invoking through the main method successfully-
But doing the same in my second class, Netbeans shows an error at the invocation line 9 "Identifier expected. package obj does not exist".
Why can't I invoke at a non- main class?
Heres the code:


The code for for my main class:

 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

line 9 "Identifier expected.


That type of statement must be inside of a method or static block.

When do you want that statement to be executed?
 
Doug Mann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

line 9 "Identifier expected.


That type of statement must be inside of a method or static block.

When do you want that statement to be executed?


Thank you for your reply,
the statement should be executed immediately when running the project.
Would you please show an example of this inside a method?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

statement should be executed immediately when running the project.


The first statements executed when a program is started is the main method for the starting class (the class named with the java command).

show an example of this inside a method?


See lines 10 and 11 in JavaApplication1 for an example of calling a method from inside of another method
 
Doug Mann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

statement should be executed immediately when running the project.


The first statements executed when a program is started is the main method for the starting class (the class named with the java command).

show an example of this inside a method?


See lines 10 and 11 in JavaApplication1 for an example of calling a method from inside of another method



Trying to encase the statment in a method still yields no output.. (as well as encasing just the obj.dothis();)

Or am I missing something?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yields no output


Code inside of a method is not executed until the method is called.  Is there any code that calls the d method?
See line 11 in JavaApplication1 for an example of a method being called from inside of another method.
 
Doug Mann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry but I do not know.
You may be refering to using the main method as an invoker to other classes' methods?
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

using the main method as an invoker to other classes' methods?


Yes, that is where the execution of a program starts.  When the JVM starts a class via the java SomeClassName command, it calls the SomeClassName's main method.  From there the code needs to create classes and call methods.  This is for standard java SE classes.  I don't know how JavaFX works.

For any of your methods to be executed, there must be a chain of execution that starts in the original main method.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . there must be a chain of execution that starts in the original main method.

So it is good design to use the main method to start the app, and for nothing else. Once you have got the app started, the app should control itself. That makes me worry whenever I see people going on about main “classes”. That makes me think somebody has told them there is a concept that one class controls the app.

. . . and welcome to the Ranch, DM
 
Doug Mann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both!
I had a misconception the main method is just an "entry point" for the program execution. As if when the program is run, it will just "start" at the main method and continue to execute separately class by class files.

So a more correct conception would regard the main method as a launcher with chain of execution assigned in it?
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the main method is just an "entry point" for the program execution


Yes that is correct.  Execution starts in the main method.  Where it goes from there is determined by the calls made there.

execute separately class by class files.  


Only if they are called from the starting main method either directly or indirectly.


main method as a launcher with chain of execution assigned in it


I'm not sure how what that means or how it differs from what I said above.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also think about it this way: A class with a main() method can be used as the starting point for executing instructions. In fact, you can have multiple classes that have main() methods but the one that will serve as the "entry point" is the one that belongs to the class that is specified when you start the Java Virtual Machine (JVM) with the command java Someclass.

The code inside the main() method becomes a "script" of sorts, which determines what other code will get executed and the order in which they are executed.

Say you have the following classes, for example:


What happens with these classes depends on the command you issue to run Java.  If you use the command

java First

then the output will be the result of executing the statements in First.main().  None of the statements in Second.main() will be executed. The First.doAnotherThing() method will not be executed either because no code in First.main() or any code that gets executed from there ever invokes First.doAnotherThing().

However, if you use the command

java Second

then the output will be the result of executing the statements in Second.main() and this time none of the statements in First.main() will be executed. The code in First.doIt() will not be executed either because there is no statement in Second.main() that invokes the doIt() method on the object referenced by first. On the other hand, First.doAnotherThing() will be executed  this time because it is invoked on line 30.
 
Doug Mann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good example.
I previously grasped the whole main affair as just a line we must put somewhere (with blank body), while execution is performed under the hood on all the other classes in the directory, not that we need to structure the execution inside the main directly. Good to have this ironed out lol
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Doug Mann wrote:... the whole main affair as just a line we must put somewhere (with blank body), while execution is performed under the hood on all the other classes in the directory


Yeah, no, that's not how it works. Technology isn't (nor perhaps will it ever be, otherwise we humans will be in trouble) at a point where computers can read our minds and infer a desired outcome from a mass of code we give it without giving it specific instructions. An empty main() will simply result in nothing being done because it contains no instructions to execute.

Computer programs are made up of three basic structures: sequence, selection, and loops. A sequence is just a set of commands that are executed one after another. A selection or decision structure executes something if a condition is satisfied, otherwise it will do something else. A loop is like a selection only it does something again and again based on whether or not a condition is satisfied.

Your confusion came out of not understand the aspect about a program having a sequence structure. Glad we've helped clear that up for you now though.  
 
Doug Mann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes indeed, thank you!
I wonder how Python for example get code execution with no main, it just executes a like a list top to bottom?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Python for example get code execution with no main, it just executes a like a list top to bottom?


Is there a way with Python to make it start execution at anywhere else besides the first statement?

Does Python have methods/functions that can be executed in the order they are called and not in the order they are placed in the source file?
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic