answersLogoWhite

0

How can a method return an object?

User Avatar

Anonymous

12y ago
Updated: 8/20/2019

With the return statement, followed by the object you want to return. For example, in Java:

String myMethod1() {

return "abcde";

// Note: The above is shorthand notation; you can also use the new operator.

}

MyDate myMethod2() {

MyDate x = MyDate(2012, 1, 1);

return x;

// Note: This assumes you have created the class MyDate,

// With the corresponding constructor.

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How do you return an object in java?

With the command return, followed by an object variable. In the method header, you have to declare the return type as the class of the object.


Can an overridden method return void instead of an object?

Sure. An overridden method can return anything it wants.


Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


How do you search for an object in a list and if matches return that object using java?

Using boolen Contains(Object ob) method of List Interface .we can find object through List....


What is premain method in java?

A premain method is launch in java from jdk 1.5 for instrumentation. In a very simple world we can say that a premain method is used for get the size of the object resevered in heap area. It will return byte reserved by the object. This method is similar to the sizeOf() function of c/c++. In earlier version before 1.5 it was not possible to get the size of an object but after that you can get the bytes reserved by an object in heap with premain function.


How many values can a method return in Java?

A method in java can declare only one return value and type at a time. For ex: a single method cannot have a code that returns a string in some cases and an integer in other cases. Java compiler does not let you do that. You can only have one return type for every method in java.


Applet class is not inheriting AppletContext interface but in Applet Class getAppletContext method is returning AppletContext object how is it possible?

There's nothing in Java that says that a method in a class can only return object types that the class inherits from. Your Applet is running in some sort of environment (a context, if you will). The Applet.getAppletContext method returns an AppletContext object which describes this environment.


When will you define a method as static in java?

Static keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class) Ex: public class StaticTest { public static String getAuthorName() { return "Anand"; } } Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as: String authorName = StaticTest.getAuthorName();


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }


Static method can be call object?

yes we can call a static method with object


Difference between method and constructor in java?

A constructor is technically a type of method, but it is a very special type. Whereas other methods can be used to do just about anything, the only purpose of a constructor method is to create an instance of the class that contains it, often with parameters passed to it through another part of the program. This instance is called an "object" and is a central part of not only Java, but other object-oriented languages as well. A constructor method always has the same name as its containing class, and does not have a return type. Think of it this way: a class in Java is like a generic blueprint for a house. Your instance variables are like different attributes of the house - how many bathrooms will your house have, what colour will it be? Once you decide on the exact specifications for your house, you can give those parameters to the construction company, which will actually create that house. That's what a constructor method does - takes input parameters (or, lacking them, sets defaults) and creates an object.


Why use void in main of java?

No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.