Raj
Raj

Reputation: 6830

multiple classes in a single physical file on android

Right now I have to create a new physical file in eclipse android for every public class I create

For eg if I have the below 2 classes (System and Region) like this:

Region.java file:

package com.acrossair.tvguideuk;

public class Region
{
      public int RegionID;
      public String Name;
}

System.java file:

package com.acrossair.tvguideuk;

public class System
{
      public int SystemID;
      public String Name;
}

How can I simply create a file CustomObjects.java and have all of these custom classes in 1 single file?

Upvotes: 5

Views: 6797

Answers (3)

Pawan kumar sharma
Pawan kumar sharma

Reputation: 704

In java multiple class are convert multiple class file.like if your file have 3 class then 3 .class files are generated by compiler. but in android only one dex file are generated.

Upvotes: 1

user634618
user634618

Reputation: 3593

You could create your custom objects as public inner classes of a CustomObjects class:

public class CustomObjects {
  public class Region {
    public int RegionID;
    public String Name;
  }
  public class System {
    public int SystemID;
    public String Name;
  }
}

But you couldn't use static members in the inner classes unless they were static themselves.

Upvotes: 9

Peeter
Peeter

Reputation: 9382

You cannot have 2 public classes defined in a single file. You however can have multiple classes defined in a file.

package com.acrossair.tvguideuk;

public class CustomObjects {}

class Region
{
      public int RegionID;
      public String Name;
}
class System
{
      public int SystemID;
      public String Name;
}

Upvotes: 4

Related Questions