I'm having a problem importing from a java class.
Here is my program:
import MyStuff.*;
public class Monday2
{
public static void main(String[] args)
{
p("\n\n\n\t\tGood Morning from the Morning2 Class.\n\n\n");
}
}
Here is the MyStuff class:
public class MyStuff
{
public static final void p(String inputString)
{
System.out.println(inputString);
}
}
Please tell me what I am doing wrong
You forgot magic word - static, after your import like this
import static MyStuff.*;
You can read more in appropriate topic.
Hope it helps!
Instead of import MyStuff.*; just use import static MyStuff.p; For more details please also see What does the "static" modifier after "import" mean?
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
Since the main method is static, it should be able to be called statically. Therefore, I use:
package prototype.server.main;
import javax.swing.JFrame;
import prototype.server.main.gui.Swing;
public class Runtime {
public static void main(String[] argv) {
Swing swing = new Swing(true, argv[0]);
#SuppressWarnings("unused")
JFrame maingui = swing.getGuiFrame();
}
}
as the static-main code, then call using:
import prototype.server.main.Runtime;
public class Main {
Runtime.main(new String{"f"});
}
to call the static method, but Eclipse is giving me an error. Please help and thank you in advance.
There are two bugs we need to fix;
import prototype.server.main.Runtime;
public class Main {
// add constructor, or method that you can call another method
// or make this static { ... } block that fits you
public Main() {
//do not forget [] for array
Runtime.main(new String[]{"f"});
}
}
Using java version 1.7.0_05
When i compile the below code it's give me testpackage could not be found error.
But if i remove the static keyword from "import static testpackage.TestStatic;" it's compiling successfully.
Test:
import java.io.*;
import java.util.*;
import static testpackage.TestStatic;
import static java.lang.Integer.MAX_VALUE;
public class Test {
public static void main(String args[]) {
System.out.println("hello world");
System.out.println("Maximum value of int variable using " +
"static import : "
+ MAX_VALUE);
}
}
TestStatic:
package testpackage;
import java.io.*;
import java.util.*;
public class TestStatic {
public static void testStatic() {
System.out.println("Inside Test Static");
}
public void testNormal(){
System.out.println("test normal");
}
public static void main(String args[]) {
System.out.println("hello world");
}
}
import static is for importing static members of classes, not whole classes. You could say "import static testpackage.TestStatic.testStatic;".
EDIT: fixed syntax
When you say import static testpackage.TestStatic; the compiler does not know what you want to import, you could mean import a static variable TestStatic in a class testpackage. In fact, I think you wanted to import testStatic() from the testpackage.TestStatic class,
For a method or field by name
import static testpackage.TestStatic.testStatic;
For all static methods and fields
import static testpackage.TestStatic.*;
I'm trying to print my name using Java OOP Classes, Objects and Method. Below are the two scripts I'm using. Nothing is showing on the screen. I'm using Netbeans IDE.
package name;
public class Begin {
public static void displayName(){
System.out.print("Say My Name");
}
}
===========================
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
You're missing the class in your "Useaname" class declaration.
public class Useaname {
Where is Your Keyword class before your Username ?? :)
public class Useaname {
public static void main(String[] args) {
Begin.displayName();
}
}
You have to declare the method static in order to call it like Begin.displayName().
public static void displayName() {
This tells makes the method a function of the class as opposed to any particular instance of the class.
Alternatively, you can instantiate an object to call the method:
Begin begin = new Begin();
begin.displayName();
You have missed the keyword class before the class name Useaname. Here is the solution code.
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args) {
Begin.displayName();
}
}
This code should not compile.
Begin.displayName(); tries to invoke static method - you dont have such a thing :).
you have to ways - create new Begin object and invoke method on it - new Begin().displayName()
or declare displayName as static method.
There are at least two solutions to this problem:
(1) You can make the displayName() method static. This will allow you to call it with the class name.
public static void displayName() {
// snip
}
(2) You can create an object from your Begin class in order to call the non-static member method:
public static void main(String[] args){
Begin b = new Begin();
b.displayName();
}
To summarize, you should learn about objects and object creation as a beginning step to understanding Java and OOP in general.
You could use a static modifier on your displayName() method or create a new instance of your Begin class:
package usename;
public class Useaname {
public static void main(String[] args){
Begin begin = new Begin();
begin.displayName();
}
}
You haven't use the keyword "class" before "usename"
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
Correction
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
you forgot class keyword ! and you can create object for class Begin
import name.Begin;
public class Usaname
{
public static void main(String []args)
{
Begin ob = new Begin();
ob.displayName();
}
}
You forgot to add class before Username
package username;
import name.Begin;
public class username{
public static void main(String[] args){
Begin.displayName();
}
}
What you are missing here is the class keyword before Username class declaration
Since you declared the displayName() method as static using the static access modifier , there is no need to create an object of the Begin class in order to invoke the displayName() method.
In general , this is how the Username class should be :
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
Alternatively, you can invoke the displayName() using a class instance by creating an object of the Begin class, just like this:
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin b = new Begin();
b.displayName();
}
}
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.