public class DemoClass {
public void setValue(int a, int b)
{
int x=a;
int y=b;
}
public void getValue()
{
}
public static void main(String[] args)
{
DemoClass dc=new DemoClass();
dc.setValue(10, 20);
dc.getValue();
}
}
In the above program I have two methods setValue() and getValue(). SetValue method has two variables x and y which are assigned values 10 and 20(from the main method).
Now I want to display the value of x and y variables in getValue() method. But this is not possible as they are local variables. Is there any possible way of doing this?
Is there any possible way of doing this?
The usual thing is to make them fields of the class, specifically instance fields:
public class DemoClass {
private int x; // These are
private int y; // instance fields
public void setValue(int a, int b)
{
this.x = a;
this.y = b;
}
public void getValue()
{
// Use `this.x` and `this.y` here
}
public static void main(String[] args)
{
DemoClass dc=new DemoClass();
dc.setValue(10, 20);
dc.getValue();
}
}
It's relatively rare to set two separate fields with a single setValue method, but there are use cases. Normally you'd have setX and setY (and getX and getY).
You could set them as public, or if you want to use getters and setters, you can do this.
public class DemoClass {
private int x;
private int y;
public void setValue(int a, int b)
{
this.x=a;
this.y=b;
}
public void getValue()
{
System.out.println(this.x);
System.out.println(this.y);
}
public static void main(String[] args)
{
DemoClass dc=new DemoClass();
tc.setValue(10, 20);
tc.getValue();
}
}
You can also put information in a constructor function like so:
public class DemoClass {
private int x;
private int y;
public DemoClass() {
this.x = 0; // default value
this.y = 0; // default value
}
public void setValue(int a, int b)
{
this.x=a;
this.y=b;
}
public void getValue()
{
System.out.println(this.x);
System.out.println(this.y);
}
public static void main(String[] args)
{
DemoClass dc=new DemoClass();
tc.setValue(10, 20);
tc.getValue();
}
}
That way, when you create the object like:
DemoClass demoClass = new DemoClass();
It will already have those objects set. If you don't do this and accidentally call getValue() and nothing is set, you will get a nullPointerException.
Related
if I want to make a method that does something to any object that is used as input, I can write a method header that looks like this:
public static void example(Object o){...}
is there a term for "all primitive types" the same way that Object contains all non primitive variables?
I'm not sure if there is a type that encompasses only primitive types, but you can use a wrapper type and throw an error if the input given is not of one of those types.
public static void example(Object o) {
if(!(o instanceof Double)) {
throw new RuntimeException("Error Message");
}
}
This code only checks the Double wrapper type, but you get the idea.
You can use Wrapper type for primitives. Then, they become objects and you can use them wherever you can use Object.
public class Main{
static void print(Object o){
System.out.println(o);
}
public static void main(String[] args) {
Integer i = 5;
Double d = 5.5;
Boolean b = true;
print(i);
print(d);
print(b);
}
}
No, but method overloads are usually how that is accomplished. For example:
// Notice this method is private.
private static void exampleImpl(Object obj) { /* ... */ }
// And these are public, so it is only possible for an outside caller
// to pass (wrapped) primitives to the above method.
public static void example(boolean value) {
exampleImpl(value);
}
public static void example(byte value) {
exampleImpl(value);
}
public static void example(char value) {
exampleImpl(value);
}
public static void example(short value) {
exampleImpl(value);
}
public static void example(int value) {
exampleImpl(value);
}
public static void example(long value) {
exampleImpl(value);
}
public static void example(float value) {
exampleImpl(value);
}
public static void example(double value) {
exampleImpl(value);
}
There is not such thing, but you can use wrapper types with generics. Here is a simple program to demonstrate this:
public class Test{
static <T> void genericMethod(T t){
System.out.println(t);
}
public static void main(String[] args) {
Integer intNum = 10;
Double doubleNum = 36.5;
Boolean aBoolean = true;
Character character = 'a';
genericMethod(intNum);
genericMethod(doubleNum);
genericMethod(aBoolean);
genericMethod(character);
}
}
I have three classes:
Class One
public class One {
private static Two object;
public static void set_up(Two object) {
int y = object.get();
System.out.println(y);
}
public static void prn () {
System.out.println(object.get());
}
}
Class Two
public class Two {
private int x;
public int get() {
return x;
}
Two(int n){
x = n;
}
}
Class Three
public class Three {
public static void main( String[] argv ) {
One st = new One();
Two two = new Two(2);
st.set_up(two);
st.prn();
}
}
I want to change the static variable object in class Two by method set_up(Two object).
The problem is that static variable inside the class has the same name as the arguments in the method. How can I modify set_up(Two object) so I copy values from given argument to static object?
You can qualify it by using the class' name:
public static void set_up(Two object) {
One.object = object;
}
So I'm trying to edit an object's x value from a method in a different class. The real reason I'm doing this is far more complicated but I just wanted to simplify things.
I am creating a game and I want the object that belongs to the first Class to be updated throughout the game. However when I try to update it, it appears in the other class however as soon as scope is returned to the first class the x value remains 0.
I have been struggling with this for hours...
public class first {
private second Second;
public void view() {
System.out.println(this.Second.x);
}
public void newObj() {
Second = new second();
}
public void changeObj() {
Second.changeX(4);
Second = Second.getSecond();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
first First = new first();
First.newObj();
First.changeObj();
First.view();
}
}
public class second {
public static int x=0;
public second getSecond() {
return this;
}
public second(){
x=0;
}
public static void changeX(int x) {
x = x;
System.out.println(x);
}
public int getX() {
return x;
}
}
You're encountering this because of the way the assignment is done:
x=x;
Just doing this should trigger a warning message "The assignment to variable x has no effect". This is because you're not referring to the static variable x but the argument itself.
Each non-static variable exists in the context of an object. In this case x is static so the usage of this.x = x; in a static context is not possible either. The correct approach is
Second.x = x;
i tried compiling this basic code and can't get my main method to call another method. From my understanding, I don't have to create an object because both methods are in the same class.
However, it gives me the error- java: '.class' expected - when i call my method. anyone know why:
public class Main {
double x=0;
public static void main(String[] args) {
Function(double x);
}
public static double Function(double x) {
x+=5;
return x;
}
}
Look what you are doing in the main method:
public static void main(String[] args) {
Function(double x);
}
you are calling a method like Function(double x); but that is not correct, remove the type double, then you need to make the variable x static, because you are in an static context, after that just pass the argument x as parameter;
like:
static double x = 0;
public static void main(String[] args) {
Function(x);
}
Option2 is getting rid off static things and use instances...
class Main {
double x = 0;
public static void main(String[] args) {
Main m = new Main();
m.function(m.x);
}
public double function(double x) {
x += 5;
return x;
}
}
Non static variables can't be called from a static method. So call it by creating object of class in which the variable is declared.
public class Main {
double x=0;
public static void main(String[] args) {
Main mm=new Main(); //Creating Object of Class
Function(mm.x); //pass the value with object.var
}
public static double Function(double x) {
x+=5;
return x;
}
}
I am attempting to write a class that contains 2 private variables, however whenever I attempt to compile I am given error:
<identifier> expected for both setx and seti methods.
class complex
{
private double x;
private double i;
public void setx(x1) {x=x1;}
public void seti(i1) {i=i1;}
}
You have to write the data type as well (double in this case):
public void setx(double x1) {x=x1;}
public void seti(double i1) {i=i1;}
Actually the most Java way I recommend you is using the keyword this that refers to an instance variable. Moreover according the conventions name any class with a capital letter:
class Complex {
private double x;
private double i;
public void setX(double x) {
this.x=x;
}
public void setI(double i) {
this.i=i;
}
}