I'm trying to pass a string from one class to another, but not succeeding. I realized during research and trial and error that I need to have "public static void main(String[] args) {}" to be able to use the if statement, but then getY() produces an error. What can I do differently?
public class Testing {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
The other class is
class B {
public static void main(String args[]) {
String x = Klasatest2.getZ();
System.out.println(x);
}
}
Error:
Klasatest2.java:14: illegal start of expression
public static String getZ()
^
Klasatest2.java:14: illegal start of expression
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
4 errors
For starters, you can't declare a method inside of a method,
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
So you have to make sure that get getZ() method is declared OUTSIDE of main(string[] args)
Like this,
public class Test {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
}
public static String getZ() {
return z;
}
}
Also, you shouldn't have two main(String[] args) methods, as only one of them will be called unless for some reason you decide to call it yourself, which would be very strange.
So if you wanted the string to be set in class Test, you would need to call it's main method from your other class, possible like this.
Test.main(null);
Your application can only have one main(String args[]) method.
Try this:
public class Testing {
public static void main(String[] args) {
A a = new A("hy");
B b = new B(a.z);
}
public class A {
public String z;
public A (String z) {
this.z = z;
}
}
public class B {
public B (String y) {
System.out.println(y);
}
}
}
Related
Im in CS 1301 and I am struggling with some homework. I am trying to pass the parameter result to the final method so I can just write result instead of 42, and its not recognizing it. Any help would be appreciated.
public class ICalledYou
{
public static void main(String[] args)
{
ProductNumber();
ProductBinary();
}
public static void ProductNumber()
{
Double x = 7.00;
Double y = 6.00;
Double result = y*x;
System.out.println(result);
}
public static void ProductBinary()
{
System.out.println(Integer.toBinaryString(result));
}
}
The method public static void ProductBinary() does not take a parameter. Change that,
public static void ProductBinary(double result)
Second
ProductBinary();
Isn't passing a parameter, change that
ProductBinary(42.0);
Change ProductNumber to return the result, then pass that to the other method:
public static void main(String[] args)
{
double result = ProductNumber();
ProductBinary(result);
}
public static double ProductNumber()
{
Double x = 7.00;
Double y = 6.00;
Double result = y*x;
return result;
}
public static void ProductBinary(double number)
{
System.out.println(Integer.toBinaryString(number));
}
i am new to using ASM library.
1-i have to find number of class in code
2-which class called which method,(caller class)
3-each method called by which class
source code
public class Start {
public static void main(String[] args) {
Cls1 c1=new Cls1();
Cls2 c2=new Cls2();
c1.m2c1();
}
}
public class Cls1 {
int var1;
int var2;
String name;
boolean bool;
Cls1()
{
var1 = this.var1;
var1 = this.var1;
name = this.name;
bool = this.bool;
}
Cls2 cl2=new Cls2();
int z=cl2.m1c2(5,6);
public int m1c1(int x,int y)
{
return x+y;
}
public void m2c1()
{
System.out.println("From m2c1 z= "+z);
}
}
public class Cls2 {
public int m1c2(int x,int y)
{
return x+y;
}
}
i have no idea how i do this project ..
public class positiveCount {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
//This gives me the number of the positive numbers.
public static void main(String[] args) {
}
}
in the main method, i want to enter a list, for example {3,4,5,-2,-3,0},how to call the method positveCount
If the main method is in the same class :
public static void main(String[] args) {
countPositive(new int[] {3,4,5,-2,-3,0});
}
Else (works also for case 1) :
public static void main(String[] args) {
PositiveCount.countPositive(new int[] {3,4,5,-2,-3,0});
}
I have a main class and two sub class in java language.how can access xx in class Y?please help me ,I need it in my project.
import class X,Y;
public static void main(String[] args) {
xx=new X;
example=new Y;
}
public class Y{
change xx.value;//how can change it?
}
If I understand your question, then you can do it with an accessor and mutator (or getter and setter if you prefer) and something like,
static class X {
public X(int value) {
this.value = value;
}
int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String toString() {
return Integer.toString(value);
}
}
static class Y {
public void example(X x) {
System.out.println("in example");
x.setValue(x.getValue() + 5);
}
}
public static void main(String[] args) {
X xx = new X(10);
System.out.printf("xx = %s%n", xx);
Y yy = new Y();
yy.example(xx);
System.out.printf("xx = %s%n", xx);
}
Output is
xx = 10
in example
xx = 15
Create a function inside Y class with a type X parameter, then you can call this function from your Main class and change xx value.
import class X,Y;
public static void main(String[] args)
xx=new X;
example=new Y;
xx=example.changeXValue(xx);
}
public class Y{
public X changeXValue(X xx){
//change xx here
return xx;
}
}
make xx become parameter of method of class Y, because JAVA use reference by default (for object), so that if you change xx in method of class Y, it also apply to method main
import class X,Y;
public static void main(String[] args) {
xx=new X;
example=new Y;
example.modify(xx)
}
public class Y{
public void modify(X xx){ //change xx here)
}
Can i use the following code? It's not throwing any error at the Object but at obj.i. Is this a legal way of using an object? Also, how many ways can i create an object other than using the normal syntax obj s = new obj();
public class Test {
static int i;
static Test obj;
obj.i = 10; //am getting a compilation error here "Syntax error on token "i", VariableDeclaratorId expected after this token"
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
You need to place a static block around the obj.i assignment statement for this to work:
public class Test {
static int i;
static Test obj;
static { obj.i = 10; }
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
This is not, you didn't initialize. Furthermore you might not want to use static.
public static void main(String [] args) {
int i = 10;
Test obj = new Test();
obj.setI(i);
System.out.println("my objects I = "+ obj.getI());
}
now in your Test object
public class Test {
private int i;
public void setI(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}