Question

when we need to use a class name as a datatype for methods in java? explain...

when we need to use a class name as a datatype for methods in java? explain in details with helpful of examples

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Class name is used as data type in java methods in the following situations :

  1. When we need to pass instance of a particular class as argument to a method, we use the class name as data type in method declaration.
  2. When we need to return an instance of a class from a method, we mention the class name as return type in the method declaration.

Consider, the following example.

There is a class Word having a variable word and a method display.

Display method is used to print the value in word.

Another class WordTest has a method append which takes Word object as argument, appends something to it and then returns the object.

Word.java

----------------------------

public class Word {
String word = "Monica";

public void display(){
    System.out.println("Word is : " + word);
}
}

WordTest.java

----------------------------

public class WordTest {
public static Word append(Word w){
    w.word = "Hello " + w.word;
    return w;
}

public static void main(String[] args) {
     Word obj = new Word();
     obj.display();
     obj = append(obj);
     obj.display();
}

}

Output

----------------------

Add a comment
Know the answer?
Add Answer to:
when we need to use a class name as a datatype for methods in java? explain...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Create a Java project to implement a simple Name class. This class will have the...

    JAVA Create a Java project to implement a simple Name class. This class will have the following class variable: First Name, Middle Name, Last Name, and Full Name Create the accessor/getter and mutator/setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Create a main() method to test your project.

  • in java, Write methods contains and remove for the BinarySearchTree class. Use methods find and delete...

    in java, Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work.

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • PLEASE I NEED HELP WITH THIS JAVA CODE Homework 3-3 Create a Driver class to use...

    PLEASE I NEED HELP WITH THIS JAVA CODE Homework 3-3 Create a Driver class to use your Airplane and Passenger classes and create instances of them. In the main method do the following: Create an Airplane that will store up to 100 Passengers Create 5 Passenger Objects with the details specified in the table below Add the 5 Passenger objects to the Airplane Call the printDetails method from the Airplane to print all the Airplane and Passenger details. variable name...

  • Question 1 1 pts Which of the following is not a valid class name in Java?...

    Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...

  • Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer...

    Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class...

  • We use bluej for our JAVA class. If you can help me id greatly appreciate it....

    We use bluej for our JAVA class. If you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or use...

  • I need this in Java: /** * Class for storing the last ten integers added. *...

    I need this in Java: /** * Class for storing the last ten integers added. * * Your class should be named LastTen. * * Your class should implement two public methods as described below: * 1. void add(int newValue): * add a new integer to the values that we are rememebring * 2. int[] getLastTen(): * return the last ten values that were added using add, in any order. * If fewer than ten values were added, you should...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • Build a java program that has Student class, use arrays of objects {name, age, gpa} to...

    Build a java program that has Student class, use arrays of objects {name, age, gpa} to saves 3 students records. Use printable interface with abstract print method. Student class inherits from an abstract Person class that has name, age as attributes. It also has the following 2 methods: abstract setAge and concrete setGPA. Below is the hierarchy and a sample run (using netbeans): Hierarchy: Printable Interface print(Object ( ) ): object ] Abstract Person Class Name: String Age: int Abstract...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT