Access Class methods without prefixing with class - java

In a program like:
package testing;
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Mouse.pressMouse(1);
}
}
Is there a way to call Mouse.pressMouse() without prefixing using Mouse?

You can import static methods:
import static com.company.Mouse.pressMouse;
public static void main(String[] args) {
pressMouse(1); // No need to prefix with "Mouse."
}

If the method pressMouse is static, then you could Static Import your method(s),
import MarcoLib.Mouse;
import MarcoLib.Timings;
import static MarcoLib.Mouse.pressMouse;
public class Testing {
public static void main(String[] args) {
pressMouse(1);
}
}
and per the link,
So when should you use static import? Very sparingly!
If the method pressMouse isn't static, then you could extend Mouse,
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing extends Mouse {
public static void main(String[] args) {
new Testing().pressMouse(1);
}
}

Related

using a static method in a different class

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

How do I run the main method in a different file in Java?

package jav;
class PackageDemo
{
public void display()
{
System.out.println("PackageDemo executed");
}
}
public class PackageDemoDriver
{
public static void main(String[] args) {
PackageDemo boy = new PackageDemo();
boy.display();
}
}
This is the code for a package.
I will be importing this package into a different file.
The code for that is:
package exercise;
import jav.PackageDemoDriver;
class Exe
{
public static void main(String[] args) {
}
}
What should I fill in the main method to run display(), if it is possible to do so?
You can run static methods as needed
public static void main(String[] args) {
PackageDemoDriver.main(args);
}
Based off your question I assume you want the main function in Exe to essentially run the main function in PackageDemoDriver:
public class Exe {
public static void main(String args[]) {
PackageDemoDriver.main(null);
}
}
I think that'll provide the functionality you're after, if your PackageDemo and PackageDemoDriver are in different classes
Is this what you're looking for?
package exercise;
import jav.PackageDemoDriver;
class Exe {
public static void main(String[] args) {
PackageDemoDrive.main(args);
}
}

Errors when trying to use import java.util.Scanner

I'm new to coding and I'm going through an online course (MOOC). I'm on a section about reading user input and I copy-pasted the code from the website into Netbeans but I'm getting errors like illegal start of expression and not a statement. Here's my code:
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
import java.util.Scanner;
public class ProgramName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
}
}
I get illegal start of expression for the import and public class lines. I also get a not a statement error for the import line. Lastly I get illegal static declaration for the public static void line.
Any help would be appreciated. Thanks!
The import goes here:
import java.util.Scanner;
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
It is not a property of the class but a reference the compiler needs to be able to tell what "Scanner" (in this context) refers to. In this case it states that Scanner is defined in java.util which is part of the Java Runtime Environment (JRE).
Furthermore as you go on coding you should structure your files into folders.
As soon as you do so you will have to add this type of line in the first line of the file: package folderName.folderName0.
If you want to use one of your classes from another one you will have to add an import like: import folderName.folderName0.JavaApplication2
You've put an entire java source file into a main function. Simply do this instead:
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
}
}
You cannot put import and package statements inside a class. They should be done before class definitions.
Try this code:-
import java.util.Scanner;
// import before class definition.
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
public class ProgramName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
}
}
Type this in the header of the page : import java.util.Scanner;
you missing import important : import java.util.Scanner;
Exmple :
import java.util.Scanner;
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
import java.util.Scanner;
public class ProgramName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
}
}

Would not let me run the application

package aaa;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class pear {
public class aaa extends Application{
#Override
public void start(Stage primaryStage){
Button okbt = new Button("ok");
Scene scene = new Scene(okbt, 200,250);
primaryStage.setTitle("n");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}
}
This is the an example of codes that my text book had for me and I try to run it but it wouldn't run. This is the error:
Error: Main method not found in class aaa.pear, please define the main method as:
public static void main(String[] args).
I don't understand why its wrong because the main class is outside of the start class and inside of the extends Application. This is directly from a book I just need to know why it won't run.
static methods can only be declared in a static or top level class rather than in a non-static nested class. In this case its simpler just to use the latter
public class MyPearApp extends Application {
public static void main(String[] args){
...
}
}
Your main method is in an inner class (aaa). Try putting it directly in the pear class instead.
Everything should be Inside main method:
For Example:
public class pear {
public static void main(String[] args) {
//Logic and Everything else goes in here
}

Simple Java OOP Class Error

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();
}
}

Categories