Java using static void inside static int in same class - java

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

Related

How can I call a method from a separate class that contains a randomized value and use that value to print in my main class?

I am new to coding and I am trying to figure out how to call a method from a separate class and use it in my main class. I know that it is easier to do it in the main class but I was asked to do it in a separate class. The random number that is printed to range from 1 to 3 but using this method I just get 0.
here's the main class:
package example;
public class Example {
public static int number;
public static void main(String[] args) {
otherClass a = new otherClass();
a.assignNumber();
System.out.println(number);
}
}
and my separate class:
package example;
import java.util.Random;
public class otherClass {
public int assignNumber(){
Random num = new Random();
int number = num.nextInt(4) + 1;
return number;
}
}
Default value of number variable is 0 since its type is int. You need to assign the return value of function to number variable.
package example;
public class Example {
public static void main(String[] args) {
otherClass a = new otherClass();
int number = a.assignNumber();
System.out.println(number);
// Or
System.out.println(a.assignNumber());
}
}
The number variable in 'Example' class differs from the number in 'otherClass'.
The easiest way to fix your code is to fix the assignment part:
number = a.assignNumber();
But you should probably check your code again: It is better to use local int variable instead of static field.
package example;
public class Example {
public static void main(String[] args) {
otherClass a = new otherClass();
int number = a.assignNumber();
System.out.println(number);
}
}
There is no real use for the number variable.
It can all be condensed to
package example;
public class Example {
public static void main(String[] args) {
System.out.println(new otherClass().assignNumber());
}
}

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

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

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".

Declaration for an array being called into main from a method (Java)

I am fairly new to java, and am having what I assume is a simple problem with my program.
For the method arrayTest2, I cannot import it into main due to an error on compilation:
"Cannot find symbol, symbol: variable dataStorage".
I have tried also tried the declarations:
arrayTest2(dataStorage[][])
and
arrayTest2(dataStorage[5][5])`
but they don't work.
Any help would be greatly appreciated.
Cheers
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
The problem here is the scope: Something defined in one function is not visible in another. What you will normally do to solve this is to return the value. Something like this:
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
int[][] dataStorage = arrayTest();
arrayTest2(dataStorage);
}
public static int[][] arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
return dataStorage;
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
Alternatively you could have your dataStorage field as a global variable, this is however potentially very confusing. To do that you'd define
public class TrialArray
{
private static int[][] dataStorage;
// ...
public static void arrayTest() {
dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
// ...
}
on this line
arrayTest2(dataStorage);
You are passing parameter to method, that has one argument "dataStorage", but you don't declare it.
You try to pass dataStorage to your arrayTest-function, but dataStorage is not a field of the class, neither is it a local variable of main (aka dataStorage does not exist in main).
public static void main(String [] args) {
arrayTest();
arrayTest2(dataStorage); //<------- What is dataStorage?
}
Here is a little tutorial on variable scopes in Java. You probably want to return the array you created in arrayTest() and use it, but I am just guessing what you want to do.
You cant access variables in declared inside other methods.
To make it work, you would have to do this:
public class TrialArray
{
int[][] dataStorage;
public static void main(String [] args)
{
dataStorage = new int[5][5];
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}

Overriding Constants in Java

I have two classes that extend the same abstract class. They both need the same constant, but with different values. How can I do this? Some example code to show what I want to do.
abstract class A {
public static int CONST;
}
public class B extends A {
public static int CONST = 1;
}
public class C extends A {
public static int CONST = 2;
}
public static void main(String[] args){
A a = new B();
System.out.println(a.CONST); // should print 1
}
The above code does not compile because CONST is not initialized int class A. How can I make it work? The value of CONST should be 1 for all instances of B, and 2 for all instances of C, and 1 or 2 for all instances of A. Is there any way to use statics for this?
You can't do that.
You can do this, however:
abstract class A {
public abstract int getConst();
}
public class B extends A {
#Override
public int getConst() { return 1; }
}
public class C extends A {
#Override
public int getConst() { return 2; }
}
public static void main(String[] args){
A a = new B();
System.out.println(a.getConst());
}
If a constant has a variable value, it's not a constant anymore. Static fields and methods are not polymorphic. You need to use a public method to do what you want.

Categories

Resources