Global variables in java - 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();

Related

importing instance members(not static members) from a class to another class using import statement

Can i access all the members(both static and instance) in a class using like this
import java.lang.System.*;
If i want to import a class then syntax should be import java.io.PrintSteam
Then we can access printstream and for accessing static members import static should be used
import java.lang.System.* is compiled successfully but not able to access any methods,instance variables,static variables from the class,then what is imported using above line.
Lets start with why it is valid.
Quoting the JLS:
TypeImportOnDemandDeclaration:
import PackageOrTypeName . * ;
The PackageOrTypeName must be the canonical name (ยง6.7) of a package, a class type, an interface type, an enum type, or an annotation type.
The JLS says it valid to "import on demand" (wildcard import) a type, (such as java.lang.System).
Why does that make sense? Because a class (type) can have inner classes.
So when you have
public class A {
public static class InnerB
the import A.* will make that InnerB available. See here for more thoughts on this.
You can use static import like below.
import static java.lang.System.*;

Class access modifier

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

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.

Restricting access to package private variables when the package is used in other package

company xyz created a package
com.xyz.utils.
There are two classes declared in two separate files. They have some variables as package private. so that a variable X in class A can be used in class B of the same package.
package com.xyz.utils;
public class A{
int a=10;
}
package com.xyz.utils;
public class B{
int b = (new A()).a;
}
Those two files are compiled into a jar and sent to customer.
The customer add the jar to the project he is building and he writes code like below
package com.xyz.utils;
public class customer_class
{
int Y = (new A()).a;
}
Is that above code is correct?
My quetsion is. how can we make variables which are declared as package private to be not visible to others when they use the package.
The answer is "no" - you can't stop them from doing that.
It can not be done in general. I think, you can seal the package 'com.xyz.utils' in in the jar manifest, to tell the user that: do not define their classes in the sealed package as a best practice. But you can not restrict the user of your library from doing it.

Categories