0

i am new to java is there is any possible way of calling a function within same class without creating a object for the class my program is

public class Puppy{

   public Pup(String name){

      System.out.println("Passed Name is :" + name ); 
   }
   public static void main(String []args){

     public Pup( "tommy" );
   }
}

i want to call function pup without creating object for it ,is it possible?

3
  • Your comment in Pup says it is a constructor. But it isn't. It isn't a method either because it doesn't have a return type. You seem to be in a lot of confusion about Java syntax. Commented Nov 27, 2014 at 7:51
  • i know that sir but like in c/c++ is it possible to call the function without static keyword and without creating a object for it Commented Nov 27, 2014 at 8:24
  • It is not possible to call an instance method in Java unless you have an instance. This is the same in C++. If you mean standalone functions, Java doesn't have them; you have to use static methods. Commented Nov 27, 2014 at 10:41

4 Answers 4

2

You can declare the method to be static:

public static void Pup(String name){
    ...
}

As an aside: you should use standard Java naming conventions and start your method name with a lower-case letter:

public static void pup(String name){ ...
Sign up to request clarification or add additional context in comments.

3 Comments

yes sir it works , is there is any other way without using static keyword
@ArunkarthiMani - Not without creating an object. Calling a non-static method requires an object (that gets bound to this when the method is executing).
@ArunKarthiMani since you would be printing static data, why don't you just put everything in System.out in the main method directly?
1

Try this:

public class Puppy{

public static void Pup(String name){
  System.out.println("Passed Name is :" + name ); 
}
public static void main(String []args){
  Pup("tommy");
}
}

Comments

0

You can define Pup as statis method like:

public static void Pup(String name){
  // This constructor has one parameter, name.
  System.out.println("Passed Name is :" + name ); 

}

And from main call it like:

Pup("tommy");

There are couple of issues in your code:

  • You don't have valid return type for your method Pup. If you dont return anything you should add void as return type.
  • You can't use public or any access modifier within any method. So you need remove public from method call.

Comments

0

I'm not sure but maybe.... Inside your main method, you can simply use this statement,

Pup("Your Name");

Your Pup method should have a valid return statement and use these modifiers,

public static  

if it's a returning method

public static void

if it's a void method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.