Java Package level access - java

I know that class members with default access control can be accessible at package level but i'm confused about what does package level access actually mean. If default members can be accessed at package level then shouldn't i be visible in class Test2 in following example?
class 1-
package pkg1;
public class Test {
int i=0;
}
class 2-
import pkg1.Test;
public class Test2 {
void get(){
Test t = new Test();
t.i=0;
}
}
Please help me getting this concept. Thanks in advance.

Package level access means that only classes that are defined in the same package can access the package level variable. If you have to import Test, then I'm assuming that Test is in a different package and therefore it can't access i.
For Test2 to access i, define it in the same package as Test1.

You forget to write
package pkg1;
for Test2 class.
It should work now

Related

Does inheritance across packages is possible? Also, how do we compile them?

Context: Two classes from different packages (Second class in second package inherits class in first package) are connected through inheritance and made a method call to subclass from parent class.
What I did:
Written two classes in two different notepad files and trying executing one after other but was not possible for me to execute and showing error messages and my classes are as follows:
package first;
import second.Sample1;
public class Sample {
public static void main(String a[])
{
Sample1 s=new Sample1();
s.dis(1);
}
package second;
import first.Sample;
public class Sample1 extends Sample{
public void dis(int i)
{
System.out.println(i);
}
}
In Eclipse, it is giving output as 1 but in what order I should execute these codes using notepads files. Observed that compiling these classes in any order giving error messages.
Thanks much. :)
You created a cyclic package dependency, which is not a good idea.
Your base class Sample doesn't have to know anything about its sub-classes, and when it does, it is usually a sign of bad design.
Just move the main method to Sample1, and Sample class won't have to import second.Sample1.

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

Java classes and packages

Hello everybody I am a beginner of Java. I am blocked at this point with the following
program:
import prog.io.Orario;
import prog.io.ConsoleOutputManager;
class primoprogramma{
public static void main(String[] args){
ConsoleOutputManager video=new ConsoleOutputManager();
video.println("ciao");
}
}
That gives me the error:
bad class file: ./prog/io/Orario.class
class file contains wrong class: prog.utili.Orario
Please remove or make sure it appears in the correct subdirectory of the classpath.
I did everything I could tried in those days but nothing works. Here there the class
Orario:
package prog.utili;
public class Orario {
private static char separaratore=';';
}
Thank you for any advice
Your class Orario has the wrong package declaration (package prog.utili; instead of package prog.io;)
The compiler scans your import of prog.io.Orario.
It searches for class Orario in a file Orario.class in directory prog/io.
The class found has the package prog.utili declared which is not the desired one - Error
In java, directorys are the same as package names.
So, a class Orario in the package prog.utili
have to be in a directory prog/utili instead of prog/io

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.

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