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
Related
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"});
}
}
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");
}
}
This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
Closed 6 years ago.
Hello I am a want create method into main?
So this is code where i want crete method:
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}}
Java does not allow to create methods within a method. This is a general rule and NOT specific to main method.
Perhaps what you are looking for is to create additional static methods in your class:
public final class MyCommand {
private MyCommand() {
//
}
private static void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
releaseTheCatsOfWar();
}
}
Of course, it would be better to do:
public final class MyCommand {
private MyCommand() {
//
}
private void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
MyCommand that = new MyCommand();
that.releaseTheCatsOfWar();
}
}
you cannot create methods within methods in Java.
You can have it as an additional method of your class and call it form main.
public static void main is not a class, it's a method within class. And your class is input (by the way, you should start class name with an uppercase letter, like Input), in which you define method main, from which your program starts working.
Although it is entirely pointless, the closest way you would get to achieving your results is with the following:
public class Main {
private static interface MethodMaker{
void randomise();
}
public static void main(String[] args) {
MethodMaker methodMaker = new MethodMaker(){
#Override
public void randomise() {
// DO SOMETHING
}
};
methodMaker.randomise();
}
}
You have to put method outside main ,, and just call it in main ,, like this :-
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
Randomises();
}
public void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}
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.