Java multiple class definitions in the same file - java

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)

Related

Must extending class have the same access level as the class it extends

I want to extend a class that is declared public class A
It seems that if I want to create a Class B which extends class A, I must declare it with public modifier. Is it correct?
I will be happy to get an explanation why I can't make access level "stronger" when extending a class?
I encounter this problem when I tried to extends the following class:
android.support.v7.widget.RecyclerView.Adapter<VH extends android.support.v7.widget.RecyclerView.ViewHolder>
and when I extended VH, RecyclerView.ViewHolder with a class named :MyViewHolder that has private access I got a message saying: 'MyViewHolder has private access in ...'
Your code probably looks something like this:
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
...
private class MyViewHolder extends RecyclerView.ViewHolder {
...
}
...
}
The problem is that MyViewHolder is only accessible in MyAdapter's body, not in its declaration. This can be worked around by making MyViewHolder package-private, so MyAdapter's declaration can see it.
From the Java 8 Language Specification:
A member (class, interface, field, or method) of a reference type, or
a constructor of a class type, is accessible only if the type is
accessible and the member or constructor is declared to permit access:
[descriptions of public, protected, and package access]
Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level
class (ยง7.6) that encloses the declaration of the member or
constructor.

Accessing static classes in another java file

Is it possible that I can access a static class in another *.java file?
My scenario is that I have two java files in the same folder, A.java and B.java:
In A.java, which does not only include class A but static class C:
public class A {
public static void main(String[] args) {
//code
}
static class C{
//code
}
}
In B.java, which will use methods of class C:
public class B {
//code, use C's methods
}
In the file B.java, I want to use class C's methods declared in the A.java; however, the compiler does not recognize the class C, even though I put the A.java and B.java in the same folder. I suppose I do not need to import anything because both java files are in the same folder, doesn't it? Does anyone know how to solve this problem? Thank you!
Please note that class C has no access modifier and will only be visible in the same package. To clarify: A subclass won't inherit access modifiers from its encapsulating class.
If A and B are on the same packages you can access C from B by using A.C.<code> If you would like to make C available to classes outside of A's package you need to make the subclass C public:
// [...]
// insead of >>static class C {<<
public static class C {
// [...]
After you've changed the access modifier you can use C as mentioned above (A.C.<code>)

Create instance of class from other package?

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.

why class define in other package can not implement interface or extends class define in default package in java

Below interface define in default package
public interface Foo{
}
package com.code
public class MyClass implements Foo{
}
Above code will give following compilation error:
Foo can not be resolved to type
why???
That is why it is recommended that you put all your code into packages.
When you reference a class or interface without using a package name then the assumption is that the class is in the same package as the code in which it is referenced. So the compiler is seeing this:
package com.code
public class MyClass implements com.code.Foo{
}
Since there is no way to reference the default package in code then do not use it.
If you want your class to implement an interface,then you should either have both of them in the same package Or import the package which contains the interface before creating your class Or use the whole path of the interface in the declaration.

Global variables in java

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();

Categories