Class access modifier - java

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

Related

what is the difference between user defined package and system defined java package with respect to accessing static content

Q) With respect to accessing static content, what is the difference
between user defined package and java system package (say java.lang
etc)
I'm preparing for ocjp6. using 1.6.26 java version
my java program has a package named "pack" in PackageTest.java
package pack;
public class PackageTest {
public static final int i=20;
}
}
javac -d . PackageTest.java
created PackageTest.class file in pack folder
now accessing static contents of PackageTest class from another java
program (TestStaticContents.java) as below
import pack.PackageTest.*;
// here importing all contents of PackageTest class
class TestStaticContents {
public static void main(String[] args){
System.out.println("normal import, accessing i value with class name: "+PackageTest.i);
}
}
javac TestStaticContent.java
displaying Compilation Error:
TestStaticContents.java: cannot access PackageTest bad class file: .\PackageTest.java
If i try accessing static contents of Math class from my java program
its not displaying any compilation error i.e
import java.lang.Math.*;
// here importing all contents of Math class
class TestMathStaticContents {
public static void main(String[] args){
System.out.println("normal import, accessing pi value with class name : "+ Math.PI);
}
}
javac TestMathStaticContents.java
No Compilation Error, and PI value is printed as expected.
Why this behavior is different compared to User defined package?
I believe I understand your problem now. You've placed both PackageTest.java and TestStaticComponents.java in the same package, pack. Two classes that share the same package cannot explicitly import one another.
You've hinted at this yourself by providing TestStaticComponents.java with a package-private class identifier.
Your import statement works perfectly if both classes reside in different packages.
import java.lang.Math.* attempts to import the nested types from Math, not the static members. You should be using import static:
import static java.lang.Math.PI; //recommended to avoid wildcards
When using imported static members, you do not need to reference the class when using the member: You can use PI instead of Math.PI.
No Compilation Error, and PI value is printed as expected.
This is because java.lang.Math, like all types in the java.lang package, is automatically imported: there's no need to import it. Because of this, Math.PI isn't causing an error like you'd expect.
If you used PI instead of Math.PI, you would have gotten an error, informing you the static member wasn't imported.

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.

Multiple classes java

So i'm awfully new to coding but i quite like it, i'm really young so i have 0 experience on related stuff.
I'm watching this youtube series about java code and in this episode:
he creates another class and uses it in the main one but im on intelij(not eclipse as he is) and it gave me two errors saying java couldnt find the symbol (my second class);
my code:
package com.company;
public class Main {
public static void main(String[] args) {
tuna tunaObject = new tuna();
tunaObject.simpleMessage(null);
}
Second class:
public class tuna{
public void simpleMessage(){
System.out.println("Another class");
}
}
Your simple message method does not accept parameters, so don't try to pass in any. Instead of calling simpleMessage(null) simply call simpleMessage().
Also either make sure that the tuna class is located in the same package as your main class, or import the tuna class via an import statement above the Main class and below the package declaration. Even if the two source files are in the same physical directory, the Java compiler won't understand which class you are referring to unless you specifically define each class in the same package.
Adjust your second class to:
package com.company;
public class tuna{
public void simpleMessage(){
System.out.println("Another class");
}
}
Wecome to Java.
Maybe you can confirm first that the second class is located in the same package with the Main. And it is better to claim a class in First letter upper-cased format.

Java Package level access

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

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