How to handle class's from Two Different .Java Files? - java

I have two java class file's in my project.
First one is main.Java
Second one is function.java
How to call function.java method from main.java
for Example.
main.Java
public class main {
//call function here
}
function.Java
public class function {
public void example(){
System.out.println("Function working");
}
}
How to call function.java example method from Main.Java?

Class name should be always start with Upper letter.
This is the way to call a function in another class in another .java
In Main.java
public class Main{
public static void main(String[] args){
SecondClass sc = new SecondClass();
sc.go();
}
}
In SecondClass.java
public class SecondClass{
public void go(){
System.out.println("Done");
}
}
If you want to call the method in the same class
public class Main{
public void go(){
System.out.println("Done");
}
public static void main(String[] args){
Main m = new Main();
m.go();
}
}
or this
public class Main{
public static void go(){
System.out.println("Done");
}
public static void main(String[] args){
go();
}
}

First make the example() method static
public static void example(){
System.out.println("Function working");
}
Then call
public class main {
//call function here
public static void main(String[] args) {
function.example();
}
}

if they are not in the same package you should import function.java in main.java like this
import function
then you should create an instance of function class like this where you want to use it
function func = new function();
then you can call the example method using func object you created like this
func.example();

Your method isn't static, so you need to instantiate object from this class(function) to obtain access to the example method. First of all read some java specific book, so you can understand what static keyword is and how you can use it.

The code below will work :
package main.Java; //this is from where you want to call.
public class function {
public void inFunction(){
function.Java.function function = new function.Java.function();
function.example();
}
}
Happy learning :)

Related

How a static method call works in Java?

When calling out the function testFunc(), I am not using the syntax
Apples.testFunc(). Yet the code runs successfully. How so?
class Apples {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
Since, the static method is in the same class. So, you don't need to specify the class name.
If it is in different class, then you need to specify the class name.
Remember: Non-static methods can access both static and non-static members whereas static methods can access only static members.
For example :
Calling a static method present in different class, you need to do like this :
import com.example.Test;
public class Apples {
public static void main(String args[]) {
Test.testFunc();
}
}
package com.example;
public class Test {
public static void testFunc() {
System.out.println("Hello world!");
}
}
Your function testFunc() is in same class where the function main is. So you don't need to use class name before function testFunc().
When the memory is allocated for the class, Static method or static variable is assigned memory within a class. So we can access static member function or data variable using class name.
we can call static function as below
class Main1 {
public static void main(String args[] ) {
Main1.testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
or
class Main1 {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
the answer will be same, but when static function is in other class then we must use classname to call it

Unable to call function in java

When I try to call the lyrics function in this code, "invalid method declaration; return type required" occurs.
I'm learning java and very very new to it. I'm confused how to define the function and call the function so that code may run.
public class Main {
public static void main(String[] args) {
}
public void lyrics() {
System.out.println("some lyrics here");
}
lyrics();
}
Normally, one can't just invoke a method randomly in the body of the code. However, there is something called an initialization block (this gets run in the body of the constructors of the object). I think an example might clarify. Like,
public class Main {
public static void main(String[] args) {
new Main(); // <-- instantiate an instance of Main
}
public void lyrics() {
System.out.println("some lyrics here");
}
{ // <-- this is an initialization block
lyrics();
}
}
The above uses the default constructor, we can add an explicit one. Like,
public Main() {
super();
System.out.println("In Main constructor");
}
Note how the output changes.
They can also be static (and run when the class is first referenced). Like,
public class Main {
public static void main(String[] args) {
}
public static void lyrics() {
System.out.println("some lyrics here");
}
static {
lyrics();
}
}
Your code is nearly correct. Your lyrics() method must be static if you want to call it inside main method because main method is static. Non static members cannot be accessed from static method (without creating an instance to invoke it).
public class Main {
public static void main(String[] args) {
lyrics();
}
public static void lyrics() {
System.out.println("some lyrics here");
}
}
You can invoke non-static methods from static method by creating an instance of the class containing non-static method inside your main method as mentioned in comments by Elliott Frisch.
new Main().lyrics();
Since you're learning Java, you must remember that the only way to call a non-static method from a static method is to have an instance of the class containing the non-static method.
Or you can simply make your function static. Also, you need to call the function inside the main block.
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
Happy coding!
The method should be declared as static and function call should be within main method. The correct code should look like as follows:
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
You need to move the call to lyrics() into the main method where code is actually executed. Where you have it now, the compiler is expecting a new method defination.
public class Main {
public static void main(String[] args) {
// execution begins here.
new Main().lyrics();
}
public void lyrics() {
System.out.println("some lyrics here");
}
}
EDIT: created new class Main class to avoid errors.

Java moving Main class reference

I was wondering if it was a bad way of programming to move the main reference in the subclass of the software.
It will probably be hard to understand what i really mean so i will do an example.
public class Main{
public static void main(String[] args) {
Main app = new Main();
Toto myToto = new Toto();
myToto.something(app);
}
}
public class Toto{
public void something(Main app){
}
}
"this" is a non-static variable, so it can not be referenced from a static method.
so we can not call something() by using "this" keyword.
Try the below code, it is working correctly
class Main{
public static void main(String[] args) {
Toto myToto = new Toto();
Main m= new Main();
myToto.something(m);
}
}
class Toto{
public void something(Main app){
}
}

why can't I create a method in the main method [duplicate]

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

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