I cant access a class I defined from the main loop? - java

I have an extremely simple piece of code where I am calling a method that I defined, and it wont run because it says I haven't defined the method?
class Main {
public static void main(String[] args) {
go(7,3);
}
}
/// in a separate file named go.java -->
class go{
public static int go(int x, int y){
if(x <= 1)
return y;
else
return go(x - 1, y) + y;
}
}

You have two options, use the class name go to identify the class containing the method
public static void main(String[] args) {
go.go(7,3);
}
or static import it like
import static go.go;
public class Main {
public static void main(String[] args) {
go(7,3);
}
}

First thing first, Class name should start with upper case, your another class should be Go.java , to access static method
Refer Class name that is Go.go(7,3);

Related

java program have only one public class should main method only be part of that public class then any other class of that program?

program running fine when main method is inside class QNA but give error when defined inside class test
error: Main method not found in class QNA, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
import java.util.Scanner;
public class QNA{
Scanner scan= new Scanner(System.in);
private int a;
private int b;
public void get_value(int x,int y)
{
a=x;
b=y;
}
public int set_data()
{
int a=scan.nextInt();
return a;
}
public int set1_data()
{
int b=scan.nextInt();
return b;
}
}
class test
{
public static void main(String[] args) {
QNA q1= new QNA();
int p=q1.set_data();
int p1=q1.set1_data();
int c=p+p1;
System.out.println(c);
}
}
Class "test" is not an inner class of "QNA". The class "test" should be an inner class of "QNA" or it should be in its own file. And you need to target class "test", not class "QNA".

How do I call a method that has a variable passed through it?

I'm trying to call a method from within another method. I'm understanding this simply enough, until one of those methods needs a variable carried through, and then nothing I try works.
I know that I could do this in one method, but my coursework needs me to lay it out in such a way. Why doesn't this work?
public class test2 {
public static void testMethod() {
int randomNumber = 1;
}
public static void anotherTestMethod(int randomNumber) {
System.out.println(randomNumber);
}
public static void main(String[] args) {
anotherTestMethod();
}
}
You are calling a method that has an int parameter in its signature. You should pass that parameter when calling the method. I think you are trying to use a global variable, in that case, you should declare it outside any method, as a part of the class.
public class test2 {
public static int testMethod() {
int randomNumber = 1;
return randomNumber;
}
public static void anotherTestMethod() {
System.out.println(testMethod());
}
public static void main(String[] args) {
anotherTestMethod();
}
}

Java using static void inside static int in same class

public class A {
private static int B = 0;
public static void C() {
//write your code here
}
public static void main(String[] args) {
}
}
How can I increment B inside the public static void C()?
Using the Increment Operator is the easiest solution,
B++;
Or you can do it manually,
B = B + 1;
Read more,
Java - Increment Decrement Operators
import java.util.*;
import java.lang.*;
import java.io.*;
class A {
private static int B = 0;
public static void C() {
B++;
}
public static void main(String[] args) {
C();
System.out.println(B);
}
}
Although the variable 'B' is private, it is external variable and is accessible in all the methods of that class, it won't be accessible from outside of the class. so, you can increment it without any problem inside the class

This Java Program is always printing only 10 but not printing SB.Why?

public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
This code is always printing 10 but not printing SB.Why?
A static final field is implemented as a compile-time constant which is replicated in the accessing method/class without any reference to the context of definition (its class). This is why accessing it does not trigger the static class block.
This is basically due to the final keyword. If you remove final as follows:
public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello{
static int a1=10;
static{
System.out.println("SB");
}
}
You will see that SB is printed as expected.
You are using static final constant variable. This variable will be replaced at the compile time by the actual constant value, in-order to increase the performance. If you have a look at the compiled binary code (Yes you cannot ;) but technically speaking with an assumption) it will be something similar to this:
public class Test {
public static void main(String[] args) {
System.out.println(10); // Constant
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
Based on this code, class Hello will not be loaded into the RAM. So it will not print the SB.
Your static code block willbe invoked, when you create an instance of the class. Try this:
public class Test {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1 = 10;
static {
System.out.println("SB");
}
}

Getting "cannot find symbol" error while using static methods

I have really simple code, I have deleted odd code.
So this my class, one of his method is static and I would like to use it later in Main class:
public class TradeInformationReader {
private static String tradeType = "FX_SPOT";
public static double tradePrice = -1;
private double price;
public static int setTradeInformation(String path_to_file) {
return 1;
}
}
And here how I trying to call this last method:
public class Main {
public static int main(String[] args) {
String path_to_file = "D:\\1.txt";
if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
return -1;
}
return 1;
}
}
I read many posts with a similar issue, but couldn't find a solution. Everything looks fine to me. IDE doesn't show any mistakes and I'm trying just to call static method setTradeInformation, why it does not recognize it (cannot find symbol method setTradeInformation)? Any ideas? I will really appreciate your help.
Your main is not a valid main, so I guess your IDE cannot find a launching class. This should be
public static void main(String[] args)
First you have to put TradeInformationReader class in a seperate file called : TradeInformationReader.java
as follows : `
public class TradeInformationReader {
private static String tradeType = "FX_SPOT";
public static double tradePrice = -1;
private double price;
public static int setTradeInformation(String path_to_file) {
//integer to identify whether the file is found or not 1 if found and 0 if not
int isFileFound = 1;
// the code required to get the file and modify the state of the of isFileFound variable
return isFileFound;
}
}
`
then the main class should have void return type and should be in a file that has the same name as the Main Class as follows:
public class firstApp {
public static void main(String[] args) {
String path_to_file = "D:\\1.txt";
if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
System.out.println("File not found");
}
}
}
`

Categories

Resources