1

Can I put 2 public classes into one Java code?

For example: I need to turn an inputted word backwards, and then the user can ask to print out an individual character.

I have the first part of the code written, where it changes the word to be backwards, but am not sure how to implement the second part of the code within the first.

import java.util.*;

class backwards_string
{
public static void main(String args[])
{
  String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Please enter a word: ");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1 ; i >= 0 ; i-- )
     reverse = reverse + original.charAt(i);
     reverse = reverse.toUpperCase();

  System.out.println("Your word backwards is: "+reverse);

  System.out.println("Choose an individual character to print out: ");

 }
}
1
  • no, you cannot declare 2 public classes in one .java file Commented Oct 8, 2015 at 22:09

2 Answers 2

2

If the question is: how many top-level (= NOT nested) classes may I have in a single java file?

The answer is: you can have only one top-level class with the public access modifier. Also, in this case the file name has to match the name of the public class within it.

Anyway, peeking at your code:

class backwards_string { 
  //... 
}

The class backward_string is not public, it's default (the access level you obtain when you do not declare any access modifier at all). Therefore you may have as many top-level default classes as you like within the same java file.

If you really want to have more than one public top-level class, then each one has to have its own file source.

Sign up to request clarification or add additional context in comments.

12 Comments

@SSH Top level class == !(nested class). That should be it. Which part is confusing you about "top level"
@SSH In one .java file we can have only one public class, but we can have as many non-public classes (classes with default - AKA package-private visibility) as we want, like in Foo.java we can have public class Foo{...} class Bar{...} class Baz{...}.
@SSH like a top-level class is a class not contained within any other class within the source file, while a nested-class is a class declared inside some other class of the source file (within the declaration class { ... } of another class of the file).
@SSH Your teacher could see some other things wrong in your answer, which could change meaning of it, or it was not precise enough. For instance instead of class we could say type since Foo.java can also contain public interface Foo{..} interface Bar{..} class Baz{...}.
@SSH That is tricky question. If teacher meant only top level classes then answer should be false since .java file can contain only one top-level public class. But if question was about any class including nested ones then it can have many public nested classes so answer would be true.
|
0

Yes, this is possible. You don't have to put it in one file though, you can use different classes. They would have to be in the same package (folder) though.

It would look something like this:

backwards_string.java

import java.util.*;

class backwards_string
{
public static void main(String args[])
{
  String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Please enter a word: ");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1 ; i >= 0 ; i-- )
     reverse = reverse + original.charAt(i);
     reverse = reverse.toUpperCase();

  System.out.println("Your word backwards is: "+reverse);

  System.out.println("Choose an individual character to print out: ");

  //when you want to use the other class:
  DifferentAction_String otherClass = new DifferentAction_String();
  otherClass.doThingsWithReverseString(reverse);

 }
}

DifferentAction_String.java:

public class DifferentAction_String {
    public void doThingsWithReverseString(String reverse) {
        //here you do things with the reverse string.
    }
}

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.