1

As a beginner I wonder why my caller.VelocityC only works when put inside of the main block?

When i have my code like this, I can't call the method.

Method calling class:

public class Velocity2 {

VelocityCounter caller = new VelocityCounter();
caller.VelocityC(6, 3);
}

Class containing the method:

public class VelocityCounter {  
void VelocityC(int s, int v){
    System.out.print(s/v);
  }
}
TimLinden
  • 117
  • 1
  • 8

3 Answers3

12

In Java, you can't have executable statements that aren't part of a method.* The first line is okay:

VelocityCounter caller = new VelocityCounter();

because the compiler thinks you are declaring and initializing an instance variable named caller for class Velocity2. The second line, however:

caller.VelocityC(6, 3);

is illegal at the top level of a class declaration.

*Technically, that's not quite right. Statements can also appear in constructors, static blocks, and instance initializer blocks.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
4

That's because code outside of methods or constructors is only declarative. You can't put statements like assignment or method calls outside of methods or constructors.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
  • 1
    Actually, you can: `public class Test { int v = 5; { v += 2; } }` – Victor Sorokin Jul 10 '14 at 21:42
  • @Andrew_CS Are constructors not considered methods? – Jashaszun Jul 10 '14 at 21:46
  • @Jashaszun No, they aren't – JamesB Jul 10 '14 at 21:50
  • @JamesB What's the difference, except that constructors don't return anything? Constructors are analogous to a method returning `void`. – Jashaszun Jul 10 '14 at 21:50
  • 1
    http://stackoverflow.com/questions/19061599/methods-vs-constructors-in-java – JamesB Jul 10 '14 at 21:52
  • @JamesB Yes I know that but what's the practical difference for this question? In my previous statement I really meant "What's the *relevant* difference". – Jashaszun Jul 10 '14 at 21:53
  • Not much but it is important to get terminology right, regardless of the question. Constructors are not methods and I would not read any source that says they are. – JamesB Jul 10 '14 at 21:56
  • Some differences between constructors and methods: (1) constructors are not inherited; (2) the compiler will generate a default constructor if you don't define any; (3) all constructors (except for `Object`, which has special rules) must either directly or indirectly call through to a superclass constructor, and if you don't, the compiler will insert a call to the default superclass constructor--or generate a compiler error if the superclass has no accessible default constructor. Also, statements can go inside static and instance initializer blocks, which are neither constructors or methods. – Ted Hopp Jul 11 '14 at 02:42
2

That area of the source file is where you can declare fields of the class or fields of the instances, but if you still really want to call caller.VelocityC(6, 3); then you can use a instance initialization block like the following:

public class Velocity2 {

    VelocityCounter caller = new VelocityCounter();
    {
        caller.VelocityC(6, 3);
    }
}

caller.VelocityC(6, 3); would execute during every construction of Velocity2, just like the execution of VelocityCounter constructior and assignment to caller.

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

NESPowerGlove
  • 5,496
  • 17
  • 28
  • 1
    That will only execute if an instance of `Velocity2` is constructed. And it will execute every time one is constructed, which may not be what OP wants. – Ted Hopp Jul 10 '14 at 21:43
  • I assume OP knows it executes every time as he/she knew that the construction of caller happens every Velocity2 construction as well, but I can edit that info in. – NESPowerGlove Jul 10 '14 at 21:45