Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all, i want to import a class into my Main, both of them are in the same package.The package's name is Concurrency and the class i want to import is ThreadColor. I imported it into my Main with
import static ThreadColor.ANSI_PURPLE;
and i am getting this error when i compiled the code -
java.lang.ExceptionInInitializerError
. When i put my cursor on the
import static ThreadColor.ANSI_PURPLE; 
it's showing - static import only from classes and interface, cannot find symbol.
I want to use the color constants that are in the ThreadColor class in my Main (that's why i am trying to import the ThreadColor class). The Main and the ThreadColor are in the same package (Concurrency). What can i do? Thanks for any help.

ThreadColor class
public class ThreadColor {
    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_BLACK = "\u001B[30m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_BLUE = "\u001B[34m";
    public static final String ANSI_PURPLE = "\u001B[35m";
    public static final String ANSI_CYAN = "\u001B[36m";
}


What I have tried:

import static ThreadColor.ANSI_PURPLE;
import static ThreadColor.ANSI_GREEN;
Posted
Updated 2-Dec-19 16:27pm
v7

Even if under the same class; you still need to import static with absolute package path.

example:

Java
package a.b.c;
import static a.b.c.StaticStrings.ONE;
public class Main {
  public static void main() {
    System.out.println(ONE);
  }
}


Java
package a.b.c;
public class StaticStrings {
  public static final ONE="The one";
}



if you you are a newbie who is still learning and didn't declare any package
then declare package and proceed as follow:
1. create a directory name ansicolor
2. move both of the files to ansicolor directory
3. In both file declare package as 
package ansicolor;

4. from the parent of ansicolor directory run the following command
javac ansicolor/Main.java
 
Share this answer
 
v2
@Mohibur Rahid, thanks a lot, concurrency was the project's name not the package name, i just had to create the package.
 
Share this answer
 
Comments
Richard Deeming 3-Dec-19 10:02am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment. Do not post your comment as a new "solution".
UT7 3-Dec-19 11:32am    
okay, noted with thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900