Basically, here's the gist of my code:
// File: ShapeColor.java
package mypackage;
public enum ShapeColor
{
MAUVE,
PURPLE,
VIOLET;
}
// File: ShapeSize.java
package mypackage;
public enum ShapeSize
{
TINY,
SMALL,
MEDIUM,
LARGE,
HUGE,
COLOSSAL;
}
// File: Shape.java
import static mypackage.ShapeSize.*;
import static mypackage.ShapeColor.*;
public class Shape
{
private ShapeSize size;
private ShapeColor color;
public Shape()
{
this(ShapeSize.MEDIUM, ShapeColor.PURPLE);
}
public Shape(ShapeSize ss, ShapeColor sc)
{
this.size = ss;
this.color = sc;
}
}
How can I import static my enum definitions so I can access the enum values without qualifying them? I have all my .java source files slapped in one directory and I'm compiling from the command line. I have no idea about packages and how to "organize" my source files in directories (or if I even should do so). I've tried reading about these concepts but all that did was get me more confused. I learn much better through examples and demo code than by reading documentation, unfortunately.
EDIT:
After adding package mypackage; to the enum files and import static mypackage.* to the class file. I get the following errors:
.\ShapeSize.java:4: error: duplicate class: mypackage.ShapeSize
public enum ShapeSize
^
Shape.java:7: error: cannot access ShapeSize
private ShapeSize size;
^
bad source file: .\ShapeSize.java
file does not contain class ShapeSize
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Figured it out!
Moved both enum source files to sub-folder called mypackage and also normal-imported them in addition to import static their values. Shape class now starts like this:
import mypackage.ShapeSize;
import mypackage.ShapeColor;
import static mypackage.ShapeSize.*;
import static mypackage.ShapeColor.*;
EDIT:
Actually, no need to move the enum source files. Just had to standard-import them.
You can use:
import static mypackage.ShapeColor.PURPLE;
import static mypackage.ShapeSize.MEDIUM;
Related
If a source file defining a class also defines an enum used by the class (and users of the class), why does the source file require an import of that enum? I am using Netbeans 8.2 by the way.
Here is a simple example:
// file and class name Foo, enum name Bar
package pak;
import pak.Foo.Bar; // <-- why is this required?
public class Foo {
public static enum Bar { ZOT,BIF };
public Foo() {}
public void doFooBar(Bar op) { /* whatever */ }
}
Referring to the simple example above, the Netbeans compiler insists I include the import of the enum.
Why do I need to import when the enum is in the same source file as the class?
I keep getting the error cannot find symbol. Symbol: Method countAll. However, how i have it set up my professor keeps insisting this is how to have it set up.
import javax.swing.*;
import java.io.*;
import java. util.*;
public class Homework15{
public static void main(String args[]){
try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File text = chooser.getSelectedFile();
LetterCount.countAll(text);
}
catch (IOException error) {
System.out.println(error);
}
}
}
Since you are using a static method called countAll from the class LetterCount, you need to import such class, like this:
import your.package.LetterCount;
where your.package is the package where that class is located
LetterCount sounds like a utility class with static methods inside so you probably haven't imported the correct packages on top from from it looks like. Go to the class and on top copy the package path and import it in the class with the main method.
Also make sure that the methods are actually static.
As Juxhin and Jmm said: make sure to import the packages. If that doesn't work make sure your countAll method is not private. Because if it is private you won't be able to access it outside of the LetterCount class.
My teammate may have found an interesting bug in jdk compiler.
It seems to depend on import sequence when trying to compile a class with 3-level nested class.
FrameLayoutCompat.java
package com.external;
public class FrameLayoutCompat
{
}
First.java
package com.nested;
import com.nested.First.Second.Third;
import com.external.FrameLayoutCompat;
public class First
{
public static class Second extends FrameLayoutCompat
{
public static class Third
{
}
}
public static class Second2
{
public static class Third2
{
private Third mThird;
}
}
}
Try to compile FrameLayoutCompat.java First.java
javac com/external/FrameLayoutCompat.java
javac com/external/First.java
Here's the error.
com/nested/First.java:8: error: cannot find symbol
public static class Second extends FrameLayoutCompat
^
symbol: class FrameLayoutCompat
location: class First
1 error
But if we change the code by switching the import sequence. It works.
package com.nested;
import com.external.FrameLayoutCompat;
import com.nested.First.Second.Third;
public class First
{
public static class Second extends FrameLayoutCompat
{
public static class Third
{
}
}
public static class Second2
{
public static class Third2
{
private Third mThird;
}
}
}
We tried the ecj, eclipse java compiler, it works also, so it may be a bug in Oracle jdk compiler.It works. Any comment?
java -jar ../ecj-4.4.jar -source 1.8 .
[Edited]
I have try this to compile. Because other thinks it's better to compile them together. But the problem is that: the import sequence matters, instead of the 'cannot find symbol' error. Because if you try to change the sequence of import, it works well.
javac -s ../src -d ../build com\external\FrameLayoutCompat.java com\nested\First.java
Here's the zip file I saved in Evernote. It's worth a try. https://www.evernote.com/shard/s70/sh/8ed81644-5b90-4008-aa33-3b9e3aa7904a/5f8ba97f96d2132b
[edit]
Find a JPE for this, it's an know issue in JDK for javac command.It can be closed.
JEP 216: Process Import Statements Correctly
The reason is "extends FrameLayoutCompat":
When you use,
import com.nested.First.Second.Third;
import com.external.FrameLayoutCompat;
you are trying to import the class called "Third" which exists within class Second which exists within class First.
But the Second class is declared as:
public static class Second extends FrameLayoutCompat.
So it needs FrameLayoutCompat without which it can't compile. And if class Second doesn't compile, then class First won't compile too. And hence the error:
Try commenting
// extends FrameLayoutCompat
and instead use
FrameLayoutCompat layout;
and you'd find the order doesn't matter
Hope this helps.
My issue seems to be similar to this question and this one but it is a bit different.
I am making an AIDL service that is in a Library project and using it in my application. I have a class Vehicle that is in my application that I have made parcelable so that I can use it in my interface. (I would like to get a List of vehicles from my service that is in the library to my application)
Do I need a Vehicle.java in both the application and the library?
Do I need a Vehicle.aidl in both?
I had Vehicle.java AND Vehicle.aidl in both application and library and I began running into a problem in my application that when I called a method from my interface eclipse wanted me to define it as the Vehicle of the library class and not the application(although they are the same and both parcelable).
public List<Vehicle> getVehicles(){...code... }
In an effort to resolve this, I tried to make it the application's vehicle class rather than the library's vehicle class in my IRemoteInterface.aidl(in the listed variation below, i get an error that it can't find the import. In other variations like having it be List and no import, it says unknown return type).
package LIBRARY;
import LIBRARY.RelPoint;
import LIBRARY.IRemoteServiceCallback;
import LIBRARY.FleetStatus;
import APPLICATION.Vehicle;
interface IRemoteInterface {
int getPid();
void registerCallback(IRemoteServiceCallback callback);
void unregisterCallback(IRemoteServiceCallback callback);
List<Vehicle> getVehicles();
}
Here is my parcelable Vehicle class from the application :
package APPLICATION;
import java.util.Date;
import android.os.Parcel;
import android.os.Parcelable;
public class Vehicle implements Parcelable {
public static final String TAG = "Vehicle";
long vehicleID;
long trackID;
String vehicleName;
public static final Parcelable.Creator<Vehicle> CREATOR = new Parcelable.Creator<Vehicle>() {
public Vehicle createFromParcel(Parcel src) {
return new Vehicle(src);
}
public Vehicle[] newArray(int size) {
return new Vehicle[size];
}
};
public Vehicle(Parcel src) {
readFromParcel(src);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(vehicleID);
dest.writeLong(trackID);
dest.writeString(vehicleName);
}
public void readFromParcel(Parcel src) {
vehicleID = src.readLong();
trackID = src.readLong();
vehicleName = src.readString();
}
public int describeContents() {
// nothing special
return 0;
}
public Vehicle() {
}
//getter and setter methods below that I removed
}
I'm reasonably certain you need to have the Parcelable classes in the same package on both ends (which is what you end up with by using the one from APPLICATION on the library side, I would just do it the other way around). This package has to also be the one declared in the corresponding aidl file.
I'd suggest to use a subpackage like com.example.interop to make this cleaner (i.e., separate the shared objects in their own package). Both sides should then have a Java file in that package + an aidl file that declares it.
I had the same issue and got it working with the following folder and file structure:
Under src/main create a directory named "aidl". This is where Android by default looks for .aidl files
Under "aidl/" create a directory "com/mypackage/util"
Then under "src/main/aidl/com/mypackage/util" put the .aidl file of your service and a separate .aidl file of your Parcelable. If the service is called IGetCards then the aidl file must be named IGetCards.aidl and if you Parcelable is called Cards then it must be in its own file named Cards.aidl.
In src/main/aidl/com/mypackage/util/IGetCards.aidl:
package com.mypackage.util;
import com.mypackage.util.Cards;
/** Example service interface */
interface IGetCards {
/** Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
Cards getCards();
}
In src/main/aidl/com/mypackage/util/Cards.aidl:
package com.mypackage.util;
parcelable Cards;
Check each file has a package statement at the top and that the service file IGetCards has an import statement at the top importing the Cards class.
Now in the Java directory src/main/java/com/mypackage/util, put a corresponding Cards.kt file which implements the interface. Make sure Cards.kt has a package statement at the top that matches Cards.aidl.
In src/main/java/com/mypackage/util/Cards.kt:
package com.mypackage.util;
class Cards() : Parcelable {
//implement members
...
}
I have imported an Existing Java Application into my Workspace .
I see that , a class with same name is present in different packages with in the Application.
For example a class named "Status.java" is present with in
com.tata.model.common.Status;
com.bayer.frontlayer.dao.Status;
When I tried to use both of them within a class, for example as shown below
import com.tata.model.common.Status;
import com.bayer.frontlayer.dao.Status;
public class Adapter
{
}
It started giving an error in Eclipse stating
The import com.bayer.frontlayer.dao.Status collides with another import statement
Is there anyway to solve this without changing the name of the classes??
Thank you.
You can use them explicitly without importing them, so the included package name differentiates between the two:
//No imports required!
public class Adapter
{
private com.tata.model.common.Status x;
private com.bayer.frontlayer.dao.Status y;
}
You can import just one of the classes and use the fully qualified name for the other one.
e.g.
import com.tata.model.common.Status;
//import com.bayer.frontlayer.dao.Status;
class SomeClass{
void someMethod(){
new Status(); // com.tata.model.common.Status
new com.bayer.frontlayer.dao.Status(); //com.bayer.frontlayer.dao.Status
}
}
Though I think it would be less confusing in your case if you just used the fully-qualified names for both classes.
Directly apply full Class Names wherever applicable. Eg-
public class SomeClass {
public someMethod() {
com.myapp.someotherpackage.Status = "something";
com.some.other.package.Status = "otherthing";
if(com.myapp.someotherpackage.Status == com.some.other.package.Status) {
}
....
}
}