Java:Calling a method from class

sirch2

Joined Jan 21, 2013
1,037
You cannot define methods in constructors but you can call them. Your problem here is that you do not construct an object of the type "temp", the constructor only gets called when you say new temp(); which you can do in you main method.

To be honest I think this will be my last response to you. I have better things to do with my time than give private Java tutorials. There are THOUSANDS of java tutorials on the web and as I advised in post #2 above, go and work through some of them.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
You cannot define methods in constructors but you can call them. Your problem here is that you do not construct an object of the type "temp", the constructor only gets called when you say new temp(); which you can do in you main method.

To be honest I think this will be my last response to you. I have better things to do with my time than give private Java tutorials. There are THOUSANDS of java tutorials on the web and as I advised in post #2 above, go and work through some of them.
Sorry for asking this much questions.As you said I'm now looking through tutorials.Thanks for your help.
 

darrough

Joined Jan 18, 2015
86
When you have a class there is a beginning bracket and an ending bracket. In between those two brackets you can declare and initialize variables, define methods, define constructors, define static initializers, put comments and define other classes. However, you cannot executable code in this space. This will never work.:

Code:
public class MyClass {
          myObject.myMethod();
}

It would be INCORRECT, but would WORK to do something like this:

public class MyClass {

          Object myObject = myMethod();

          public static void main(String args[]) {
                    System.out.println("STARTING JAVA PROGRAM");
                    MyClass myObject = new MyClass(); 
           }

          Object myMethod() {
                   System.out.println("ENDING JAVA PROGRAM");
                    return new Object();
          }
}

The correct way is like this:

public class MyClass {

          public static void main(String args[]) {
                    System.out.println("STARTING JAVA PROGRAM");
                    MyClass myObject = new myClass();
                    myObject.myMethod();
           }

          void myMethod() {
                   System.out.println("ENDING JAVA PROGRAM");
                    return;
          }
}
Does this answer your question?

Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

Thread Starter

justin2014

Joined Nov 26, 2014
24
When you have a class there is a beginning bracket and an ending bracket. In between those two brackets you can declare and initialize variables, define methods, define constructors, define static initializers, put comments and define other classes. However, you cannot executable code in this space. This will never work.:

Code:
public class MyClass {
          myObject.myMethod();
}

It would be INCORRECT, but would WORK to do something like this:

public class MyClass {

          Object myObject = myMethod();

          public static void main(String args[]) {
                    System.out.println("STARTING JAVA PROGRAM");
                    MyClass myObject = new MyClass();
           }

          Object myMethod() {
                   System.out.println("ENDING JAVA PROGRAM");
                    return new Object();
          }
}

The correct way is like this:

public class MyClass {

          public static void main(String args[]) {
                    System.out.println("STARTING JAVA PROGRAM");
                    MyClass myObject = new myClass();
                    myObject.myMethod();
           }

          void myMethod() {
                   System.out.println("ENDING JAVA PROGRAM");
                    return;
          }
}
Does this answer your question?

Moderators note: Please use code tags for pieces of code
Thanks for replying to this thread.To be frank I couldn't actually get the code:
Code:
  Object myObject = myMethod();
It might be because I'm weak in java.I think that as you said you can't call a method directly from class in normal situations but you might using static modifiers.
 

Thread Starter

justin2014

Joined Nov 26, 2014
24
You can call the method directly if you are initializing an instance or class variable. myObject is an instance variable.
Thanks.Now I think that means the problem lies with intialization.So only you could execute a method by instantiating it otherwise it should be static isn't it?
 

vpoko

Joined Jan 5, 2012
267
Thanks.Now I think that means the problem lies with intialization.So only you could execute a method by instantiating it otherwise it should be static isn't it?
Yes, if you're calling a non-static method, you have to call it on an object (an initialized instance of a class) and not on the class itself. If you want to call a method without creating an instance of a class, you should make the method static.
 

darrough

Joined Jan 18, 2015
86
When you are initializing an instance variable you can call a static or non static method. However this should be a rare exception. It works, but it is usually bad code. There is no other circumstance where you can call a method outside of another method. The main method is one starting point. There are others, such as the Spring framework or the use of a containet, such as Tomcat.
 
Top