100k views
3 votes
Write a statement that declares a PrintWriter reference variable named output and initializes it to a reference to a newly created PrintWriter object associated with a file named "output.txt".

User IpsRich
by
3.5k points

1 Answer

3 votes

Answer:

PrintWriter variable = new PrintWriter("output.txt");

Step-by-step explanation:

There are two ways of declaring a reference variable in programming.

One way is

Reference Variable-name = new Reference ("some texts here");

While the other is

Reference Variable-name;

Variable-name = new Reference ("some texts here");

Both ways are valid ways of reference variable declaration.

In the question above, the reference is PrintWriter, it points to output.txt and the question says "write a single statement";

Hence, we make use of

PrintWriter variable = new PrintWriter("output.txt");

A reference variable is declared to be of a specific type and that type can never be changed.

User Dupinder Singh
by
3.1k points