I have
public class TestApp { // TestApp.java
public static void main(String[] args) throws Exception {
TestClass x = new TestClass(); // compiler error
}
}
and
public class TestClass { // TestClass.java
public TestClass() {
}
}
TestClass was created by right clicking the project and clicking Add Class.
I'm using NetBeans
Assuming you didn't declare a package for either class:
The reason it worked after you placed them in the same folder is that they both became part of the same package.
This is the default unnamed package. All source files without a package declaration, in the same source folder, belong to that folders default package. Also, they cannot be imported for use in other packages.
It's useful for small test apps, but you should strive to put all classes in a proper package.
classes are known by their name, their full name !
com.myapplication.mymodule.Myclass
you can use the short part provided you :
use an import statement : import com.myapplication.mymodule.Myclass
use the short name in a class with same package
from your sample, before using import, you can to use full name :
com.test.TestClass x = new com.test.TestClass(); // change "com.test" for the real package
Related
public class Main {
public static void main(String[] args) {
}
}
i want to add package how to add it in intellij idea. Is the problem in the beginning of creating a new project ?
Lets assume you have an existing class, and you want it to be in a package. This is called refactoring
Right click on your class name (Main in this case)
In the context menu expand the "refactor"
In the refactor menu select "move to ..."
Then a dialog should appear that lets you enter the name of the package you would like to move your class to.
If it is a new package, it will create a folder and move the source file there.
Your code in file named Main.java
Just add two directories named com and codewithmosh, respectivly inside each other and then move your Main.java class inside it.
After that you can add package statement to your code:
package com.codewithmosh;
public class Main {
public static void main(String[] args) {
}
}
Direcory structure for your project is:
────project
└───com
└───codewithmosh
└───Main.java
I am creating a package com.XXXX
Inside that I am declaring many classes in a java file (default - not public)
let is be like:
class A{}
class B{}
class C{}
I am importing com.XXXX in another file
I am unable to use these classes that are inside the package, as they are not public.
So I am pushing to a state of creating individual files for each classes.
Every class is just a small structure, where it doesn't have any extra function. So I thought of keeping them in single file. So I can't declare classes as public
Is there any way to use all classes without splitting them into separate files?
If (somehow) it makes sense to group these classes together, then you could put them inside a wrapper class as static inner classes, for example:
package com.somewhere;
public class Utils {
public static class DateUtils {
public static Date today() {
return new Date();
}
}
public static class FilenameUtils {
public static String stripExtension(String path) {
return path.substring(0, path.lastIndexOf("."));
}
}
}
Then, you will be able to import these elsewhere:
package com.elsewhere;
import com.somewhere.Utils;
public class Task {
Date today = Utils.DateUtils.today();
}
This is because the default java access modifier is package private. When you create the classes without an access modifier they become accessible only within the package.
If you don't want to expose it outside the jar then rename your package to be com.internal.XXXX. When you put the internal keyword that package wont be available outside the jar even thought your classes inside the package would be public.
If you want to have more than one class in the same file, you can use Nested Classes.
Please, take a look at:
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Example:
class OuterClass {
...
class NestedClass {
...
}
}
You can then refeer to OuterClass.NestedClass from another package
I have a Main class. I also have another class I have made (call it the "Runner" class). I would like to, however, have more Runner classes (the difference between them being modifications to each respective Runner class by other people). Would it be possible to have each Runner class in separate folders and have the folders in the same directory as the Main class, and then reference the Runner objects in the Main class as if they were a class in the same directory as the Main class?
Yes, this question strikes me like you're viewing Java like php or ruby, etc which isn't quite accurate. With Java, each class has what's called a "Fully Qualified Name" which is derived from the folder it's stored in (like Python). Because Java is compiled, you tell the compiler where to find the code needed to compile a java file, rather than putting a reference to those needed files in the top of the file (like a require statement in php) which would be an instruction to the interpreter.
Say for instance you had a structure like this:
src/
Main.java
/runner1
SomeRunner.java
/runner2
SomeOtherRunner.java
Main.java
public class Main{
public static void main(String[] args){
//use SomeRunner
runner1.SomeRunner.run();
//use SomeOtherRunner
runner2.SomeOtherRunner.run();
}
}
SomeRunner.java
package runner1;
public class SomeRunner{
public void run(){ ... }
}
SomeOtherRunner.java
package runner2;
public class SomeOtherRunner{
public void run(){ ... }
}
Alternatively, you can use imports:
Main.java
import runner1.SomeRunner;
import runner2.SomeOtherRunner;
public class Main{
public static void main(String[] args){
//use SomeRunner
SomeRunner.run();
//use SomeOtherRunner
SomeOtherRunner.run();
}
}
In this example, I chose arbitrary package names (runner1 and runner) which is technically fine. However, best practices state that your package names should be the reverse domain name of your project (i.e. com.myproject) to avoid collisions with other libraries etc. You can read more about that here. When you compile your Main class, you will need to supply the SomeRunner and SomeOtherRunner files to the compiler so it knows where to find them.
I am extremely new in Java and I couldn't get one thing straight. I have E:\Java\ACP\Cricket\ directory where I have a Main.java, Player.java and CricPlayer.java. Now I am inheriting Player class into CricPlayer.
I have Main.Java as
import Java.ACP.Cricket.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello.");
}
}
CricPlayer.java As...
import Java.ACP.Cricket
public class CricPlayer extends Player
{
}
I need to use objects of CricPlayer in Main.java. As I mentioned earlier I am real new to Java so there might be some really obvious mistakes that I don't know about. But the question is how can I create a package for use in CricPlayer.java and Main.java? Because each time I compile the code using CMD it says "CricPlayer.java:6: error';' expected import java.ACP.Cricket.*" and points ^ to *. I don't know if I haven't created the Package properly or if it's some syntactical issue.
you need to add package declaration in the beginning of your code files. And you don't need imports if all the classes reside in the same package. BTW, good coding convention in java recommends that all letters in package name are lowercase :)
package Java.ACP.Cricket;
public class Main
{
// Your class contents
}
in the class below you shourd write a package (folder in project where file is situated)
for example
package ACP.Cricket;
You forgot the ; at your import in CricPlayer class:
import Java.ACP.Cricket.*;
Now you should be able to create CricPlayer Objects in your Main Class.
Also I don't know if "Java" in your package name is supposed to be capitalized or not. Seems like the compiler is expecting a lower case 'J' from your naming convention
Here's an alternative to your Programs.I've verified this myself, so you can be assured that it will work.
Important thing you need to know, is that any file can be compiled only if you have a main method for that file(ie in that class). so here's a detailed 'How to do it':
PlayerDemo.java:
package Java.ACP.Cricket;
class Player{
//class members & methods
}
class PlayerDemo{
public static void main(String[] args){
System.out.println("Player Class");
}
}
CricPlayerDemo.java:
package Java.ACP.Cricket;
class CricPlayer extends Player{
// class members & methods
}
class CricPlayerDemo{
public static void main(String[] args){
System.out.println("CricPlayer Class");
}
}
Main.java:
package Java.ACP.Cricket;
class Main{
public static void main(String[] args){
System.out.println("Main Class");
//use other class objects here directly.
}
}
In E: , do following stepwise:
1> javac PlayerDemo.java (this creates Player.class & PlayerDemo.class)
2> copy Player.class to Cricket folder.
3> javac CricPlayerDemo.java (this creates CricPlayer.class & CricPlayerDemo.class)
4> copy CricPlayer.class to Cricket folder.
5> javac Main.java
6> copy Main.class to Cricket folder.
7> now,also in E:, java Java/ACP/Cricket/Main
Instead of compiling from E:, you can also compile from Cricket,to avoid copying class files,but it wont work for CricPlayerDemo.java, as CricPlayer extends Player(whose location is acc to package) so you'll have to compile CricPlayerDemo from E:
Vincenzzochi is right, packages are lower case (but it should not break compilation).
And you need to declare the package at the begining of all your classes :
package java.acp.cricket;
And while all your source files are in the same package, you don't have to import anything!
A.java
package a;
class A {
void f1()
{
System.out.println("hi");
}
}
B.java
In same folder as of A.java and class files are generated in the same folder.
package b;
class B
{
new A().f1();
}
I am new to package concept. My question here is even though they are not in the same package how can B create the object of A?
If you use public class declaration it goes to public access level. Public classes can be accessed by different packages by using import a.A; or providing the complete class name new a.A().f1().
B will import (or can use fully qualified name) the class A from other package if it has to use it. importing enables the classes in other packages to see the classes from a different package. But this can be further find-tuned by using scoping.
If both are in the same folder and not using any packages explicitly then they both are in the default package. And you no need to explicitly import the classes while using them if they are in the same package.
You should know access modifiers in java. If your class is public ,it can be accessed anywhere simply by importing it.
Please go through below link for better understanding
http://javapapers.com/core-java/access-modifiers-in-java-explain/
There is something called import in Java which you can import into current package and access methods or members as long as they are public(visible across package) or protected (visible to sub-classes)