I just made two classes with one package, but when I run program it says 'Could not find or load main class', but when I remove package p; from main program everything is right.package p;
package p;
public class Ex5 {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Whats wrong with that package?
package p;
public class Help {
int x;
int y;
public void Help(int a, int b)
{
x = a;
y = b;
}
}
in creating a package you must follow the java naming convention
to create a package you must name it using the company website in reverse order
e.g. if your company is dawidyenko.com you should create a package com.dawidyenko;
after creating a package you can then import it in your main method using
import com.dawidyenko.Help;
note that your package is not like java built in packages so you must include the class name and must not use the * wild card symbol
This will work for you. You can get everything from the Help class.
import p.Help;
public class Ex5 {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Related
Let us suppose we have a class:
package package1;
public class Car {
public static int brake()
{
int x;
//some functionalities
}
//other functionalities
}
I want to ask for using this method brake in classes of different package, do we need to include package name also? -- int xyz=package1.Car.brake(); or simply Car.brake(); will work
You can import package or use full path of method:
First solution:
public class App {
public static void main(String[] args) {
package1.Car.brake();
}
}
Second solution:
import package1.Car;
public class App {
public static void main(String[] args) {
Car.brake();
}
}
Add import statement like import package1.Car; and then you can use Car.brake(); to call the function. Read more about imports here
I'm trying to get object from another class from another package
package processManager;
public class PCB {
public int vruntime;
public int nice_value=0;
}
in the same package
package processManager;
public class Process {
public Process(PCB pcb) {
this.pcb = pcb;
}
public Process() {
}
public PCB pcb;
int a;
}
usage of object
package processManager.newpackage;
import processManager.Process.*;
public class NewClass {
public static void main(String[] args) {
Process proc=new Process();
}
}
and I don't know why but then I've got "Process is abstract; cannot be instantiated"
Please look closer at your code:
A) You have defined a class processManager.Process which is not abstract.
B) Next in the next file you are importing
import processManager.Process.*;
Which actually defines an import of all sub-classes of processManager.Process class (you have none) but the class itself is not considered an import.
C) This means that in the next piece of code
Process proc = new Process();
You are trying to create an instance of java.lang.Process class which is abstract.
This is a source of your error.
This question already has answers here:
How to access java-classes in the default-package?
(5 answers)
Closed 8 years ago.
I write a normal class
public class TestAccess {
}
class T{
private int i=0;
int j=0;
protected int k=0;
public int m=0;
}
class TT
{
public void m(){
T t= new T();
t.j=9;
}
}
Then I access it in the same directory with another class
public class TestProtected extends T
{
public void method(){
System.out.println(k);
}
public static void main(String[] args)
{
System.out.println("HW!");
}
}
But when I use package in the second class, there is something wrong to access class T;
package m;
public class TestProtected extends T
{
public void method(){
System.out.println(k);
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Can you guys tell me how I can access class T in package m?
The issue here is not related with public, I have tried, to be more general, what if you only have class T instead of its java code? The situation is you can access the T class without package, but you cannot access it with package.
You should make your class T public. Otherwise, it is not visible to classes in different packages.
You'll have to move T to a separate file, since you can't have multiple top level public classes in the same file.
Finally, your TestProtected class located in pacakge m should import the T class.
Package 1:
package Mypackage;
public class MyFirst {
int a;
MyFirst()
{
a=10;
}
}
Package 2:
package mySecondPackage;
import Mypackage.MyFirst;
public class MySecond extends MyFirst{
public static void main(String[] args) {
}
}
I got one error that is The import Mypackage cannot be resolved.
Then in the classpath of second project, add first project..
It must resolve your problem :)
Steps to fix it,
Open project properties(ALT+Enter) -> left side check for java build path --> Project tab right side and then add first project..
your superclass constructor is not visible that's why it is giving error..try this...
package pack1;
public class MyFirst {
int a;
public MyFirst()
{
a=10;
}
}
package pack2;
import pack1.MyFirst;
public class MySecond extends MyFirst{
public static void main(String[] args) {
MySecond s= new MySecond();
}
}
I have a question on class imports, It seems you can call a method with a reduced line if you have imported the class. I don't understand what is the name of this operation, and how is it possible...
For instance :
Why this code
public class test
{
public static void main (String args[])
{
System.out.print("Test");
}
}
Can be replaced by
import static java.lang.System.out;
public class test
{
public static void main (String args[])
{
out.print("Test");
}
}
What happens if you have also an object named "out" ?
Thanks in advance
What happens is that out from external class must be referenced by full name:
String out = "Hello World";
java.lang.System.out.println(out);
The variable out will shadow the static import and you will have to use the full name in order to use the function print.
import static java.lang.System.out;
public class Tester5 {
public static void main (String args[]) {
int out=0;
out.print("Test");
}
}
yields "cannot invoked print(String) on primitive type int. The same error is shown if out is an object.