I have 2 packages, airline and userInterface
In the AirportMain I am trying to create an instance of class AirlineReservation but it is from package userInterface, unlike my AirportMain.
I'm using:
AirlineReservation airlineReservation = new AirlineReservation();
but getting an error that it is not public in AirlineReservation so it cannot be accessed outside the package, but I do have:
public class AirlineReservation {
in class AirlineReservation.
What am I doing wrong here?
If the constructor for AirlineReservation is not declared public, that is your problem there. Make the constructor public.
If it is public, it's possible you need to put
import userInterface.AirlineReservation;
at the beginning of AirportMain.
Make the constructor public and for more information about access modifier link.
Related
I have the classes created, I'm trying to
FilePostProcessFactory PostProcessFactory = new FilePostProcessFactory();
FilePostProcess filePostProcess = PostProcessFactory.getFilePostProcessName(fileName);
filePostProcess.getFileConfig(fileId, postProcessInstructions);
This method: getFileConfig is giving me an error: is not public in packageName'. Cannot be accessed from outside package
I was reading this:
https://www.javatpoint.com/factory-method-design-pattern
and they have implemented there that the abstract void getRate(); can be accessed from another class outside of the package.
What am I missing?
Thank you
Change the package of a class from which you're calling it to be the same as the package of the target class, then the error will dissappear. You could also use reflection, and call setAccesible(true) on the method reference, then invoke it.
Try declaring the class public or do what Krzysztof mentioned.
I ended up renaming the class: public abstract class myClassName{...}
That fixed it
I have the following two classes
package one;
public class Student
{
//Some code
}
package two;
public class Test
{
public static void main(String args[])
{
Student s = new Student();
//Some code
}
}
Even though the "Student" class has public access modifier, whenever I try to create an object for the Student class, in the Test class, which is from another package, eclipse points out error saying either I need to import the student class or create a new class.
I thought if a class is declared public, it can be accessed from any where. But why does eclipse call it an error?
You don't have an import statement, so the compiler doesn't know that Student is meant to refer to one.Student. You can either use:
import one.Student;
or
import one.*;
... or just fully-qualify the name when you create the object:
one.Student s = new one.Student();
This isn't a matter of accessibility - it's the compiler not knowing how to resolve the identifier Student into a fully-qualified class name.
If the class is in another package, you need to import it, like this
import one.Student;
and then you will able to use it. This is to avoid ambiguity if any other class in the build path has the same name.
If a class is public it means you can use it outside its package in another package. You sill need to import the class though.
import one.Student
If a class is package private it can be used within the same package but cannot be imported by other packages
I knew that when a class has an inner class then this class will be compiled to two class files. Today I have codes as below
public class GenericDeserializer {
public static void main(String[] args) {
String cityPageLoadJson = "{\"count\":2,\"pageLoad\":[{\"id\":4,\"name\":\"HAN\"},{\"id\":8,\"name\":\"SGN\"}]}";
Type type = new TypeToken<GenericResult<City>>() {
}.getType();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
GenericResult<City> cityPageLoad = gson.fromJson(cityPageLoadJson, type);
for (City city : cityPageLoad.getPageLoad()) {
System.out.println(gson.toJson(city));
}
}
}
Although the above one has no inner class, java compiler still creates two class files:
GenericDeserializer.class
GenericDeserializer$1.class
Using Java Decompiler tool, I see content of the second
package net.tuandn.training.lesson.gson;
import com.google.gson.reflect.TypeToken;
import net.tuandn.training.model.City;
import net.tuandn.training.model.GenericResult;
final class GenericDeserializer$1 extends TypeToken<GenericResult<City>>
{
}
Could anybody please explain why this class is created?
And when are multiple class files created on compiling?
Thank a lot!
Two class files are generated because you are using an anonymous class in the following statement:
TypeToken<GenericResult<City>>() {
.....
}
Each anonymous class file uses the same name as of the container class and appends a $1/$2 and so on.
new TypeToken<GenericResult<City>>() {
}
creates an anonymous inner class. Anonymous inner classes, just like inner classes are compiled to separate class files. Since anonymous class don't have name, that is why numbers are used to generate unique names for each such classes. The number you see there after $ is the numbering for that anonymous class, as they come in order in your enclosing class.
If you use more anonymous classes like that, the number will increment by 1. So for more anonymous classes, the generated class files would look like:
GenericDeserializer$1.class
GenericDeserializer$2.class
GenericDeserializer$3.class
GenericDeserializer$4.class
....
For inner classes however, the value after the $ is the name of the inner class, as you already would have noticed.
And when are multiple class files created on compiling?
Yes, those classes are generated, when you compile your top-level class.
Simple enough, your decompiled class shows
final class GenericDeserializer$1 extends TypeToken<GenericResult<City>>
So you have a TypeToken<GenericResult<City>> somewhere.
Looking through your code we see
Type type = new TypeToken<GenericResult<City>>() { /* anonymous inner class */ }.getType();
There's an anonymous inner class declaration there which will therefore get its own class file with $X suffix.
just a simple question, if you have the following code in a file called Example.java:
package MyPackage;
public class Example{
void foo(){}
...
...
}
class A{}
class B{}
what is the modifier of the class A and B?
They have the default modifier, which means package-private.
See here
They are both default visibility (package private).
Controlling Access to Members of a Class (Java Tutorial)
I want to make a class in java that is accessible to all other classes in my project.
I created this in the default package and now it cannot be seen. What is the best way to do this in java?
Typically the default package is not used, your package would be something like com.yourdomain.mypackage. As long as you declare the class as public, it can be seen by all classes as long as they import it.
The class would look like
package com.mycompany.mypackage;
public class MyClass {...}
Then the user of the class would be
package com.mycompany.anotherpackage;
import com.mycompany.myPackage.MyClass;
private final MyClass myClass = new MyClass();