The moose likes Beginning Java and the fly likes method invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method invocation" Watch "method invocation" New topic

method invocation

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
I have two classes Test1 and Test2:

public class Test1 {

public void doFooStuff() { }


public class Test2 extends Test1
public void doTest2Stuff() {


Test2 t2 = new Test2();
t2.doTest2Stuff(); // syntax error..

public static void main(String args[])
System.out.println("inside the main methjod");


Can someone please let me know why it errors. If i mention Test2 t2 = new Test2();
t2.doTest2Stuff(); inside main() or inside doTest2Stuff() it doesnt error.

Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3786

You are getting an error on that statement because it doesn't belong to a scope. Statements should reside in a scope - inside a method, constructor, or an initialization block. Please UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
pankaj vijay
Ranch Hand

Joined: Apr 01, 2008
Posts: 75
jose a method execution starts from main method not from else where.

you are writing code that can not be reached from anywhere that's why its is showing error

Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
I agree. Here's the link:
subject: method invocation
It's not a secret anymore!