Method calls inside a Java class

how to call a method in java from the same class
how to call a method in java from another class
how to call a method from another class file in java
calling methods in java
how to call a method in java from main
how to call a void method from another class in java
how to call a class in java
java method class example

I was setting out to write a piece of code today in Eclipse, and I started out like so:

public class MyClass {

(Ignore the fact that I wasn't thinking straight, etc. etc...)

I get this compile error:

Syntax error on token "currentTimeMillis", Identifier expected after this token

It works if I change that statement to an assignment statement:

long time = System.currentTimeMillis();

Of course, it doesn't cause errors if placed inside a method body and also within blocks inside the class body.

Why is this? Is there some compiler level rule that says that only assignment statements or declarations should be present inside the class body?


The class body can only contain declarations.

Specifically, § 8.1.6 of the JLS defines the class body like this:

A class body may contain declarations of members of the class, that is, fields (§8.3), classes (§8.5), interfaces (§8.5) and methods (§8.4). A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class.

      { ClassBodyDeclarationsopt }
      ClassBodyDeclarations ClassBodyDeclaration

As you can see, there are no statements in there anyway, so a class body may not directly contain a statement.

If you think about it, it makes sense: at which point should that code be executed? There is no context to tell you about that, so it makes no sense.

How to call a method in another class of the same package?, In this program, you have to first make a class name ' CallingMethodsInSameClass' inside which you call the main() method. This main () method is further calling� However the way the method plays sounds is it calls a method that plays a single sound (playSadMusic, playHappyMusic, etc). Those methods are in an interface that I have to create a mock object for. I'm a little stuck on how I would exactly go about testing this.

This is illegal. In the class body you can have only: blocks, fields, constructors, methods and classes

Yours is neither. And what would you expect it to do anyway? If you want it to be executed when the class is instantiated, then place it in a block:


Java Program to Call Method in Same Class, How to call methods in java. We can then put all the methods there, instead of clogging up the main class. (You'll start learning more about classes in the next� To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon (;). A class must have a matching filename ( Car and ). Using Multiple Classes

Try this way:

public class MyClass {
    static {

If you call System.currentTimeMillis() inside a static statement it works. The static block will be called when the class "MyClass" is loaded by the class loader.

java for complete beginners - method calling, If a method is protected , then only this class and subclasses (classes that use this as a basis to build off of) can call the method. If a method is� Maybe I'm a bit tired, but I can't see how the call to add() inside the add() method works. I keep thinking that it should have a reference, or be a static method (and I can't find a static add() in ArrayList or List).

Is there some compiler level rule that says that only assignment statements or declarations should be present inside the class body?

In a word: yes. At the class body level you can have instance and static member variable declarations, method declarations, nested classes, object initialization blocks, static initialization blocks, and comments. That's it, by definition.

The "compiler level rules" for a language are called its grammar.

How to call a method in Java, The second keyword, static means that the method belongs to the class and not any instance of the class ( object ). Static methods must be called� FeeCalculator class has method getFee() with two arguments. public String getFee(Dog dog, Scanner input){} you need to pass the arguments in order to invoke it. change getFeeStatement.getFee(); to getFeeStatement.getFee(dog,input ); also variable names are different, FeeCalculator getFeeStatment = new FeeCalculator(); and getFeeStatement.getFee(dog, input);.

You need to call your code from inside a method, not just on its own like that. E.g.

public class MyClass {
    public static void main(String[] args)

The above still won't do anything, but it's legal :-)

How to Call a Method in Java: 7 Steps (with Pictures), A class created inside a method is called local inner class. If you want to invoke the methods of local inner class, you must instantiate this class inside method. When compiling class initializers and class initialization blocks, the Java compiler stores the compiled bytecode (in top-down order) in a special method named <clinit> (). The angle brackets

Method within method in java, Instance Method. Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have � But calling the main() method from our code is tricky. It can lead to many errors and exceptions, such as:. The main() method must be called from a static method only inside the same class.

Static methods vs Instance methods in Java, Java - Methods - A Java method is a collection of statements that are grouped There are two ways in which a method is called i.e., method returns a value or public class ExampleMinNumber { public static void main(String[] args) { int a� A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++, and Python.

Java - Methods, Method Calls in Java When a method is called a new stack frame for it is added to methods: getVal: Foo object: Foo object: : names that are in scope. In order to exist within a Java program, a method has to exist inside a class. A Java program can have many classes and each class can have several methods. And, one class in every program contains a main () method. The main () method is crucial as it tells the program where to start.

  • Funnily enough, I had gone through the page ( and section 8.1.6 before posting here. I thought it was just an indicator of the typical statements found inside a class body. Didn't realize I was going through the formal specs :D. Thanks for clearing it up.
  • @java noob feel free to mark as accepted the answer you like best
  • But then, calling System.currentTimeMillis() might have side effects which are desired, even if you are not interested in the return value. Though I don't think this is the case here.