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;
}
Related
How can I call getDummy from main? I need this so I can pass dummy to a method in another class.
public class Test {
public static void main(String[] args) {
private int dummy = 0;
}
public int getDummy() {
return dummy;
}
}
getDummy is an instance method so you need the instance
public static void main(String[] args) {
Test t = new Test();
t.getDummy();
}
and this belongs to the class
private int dummy = 0;
your final code could look like>
public class Test {
private int dummy = 0;
public static void main(String[] args) {
Test t = new Test();
t.getDummy();
}
public int getDummy() {
return dummy;
}
}
Is this what you mean?
public class Test {
private int dummy = 0;
public static void main(String[] args) {
Test test = new Test();
int dummy = test.getDummy();
}
public int getDummy() {
return dummy;
}
}
I assume private int dummy = 0; is a property (variable) of Test class. Calling a non-static method from a static method is not allowed. You create an instance of your class in the static method and can call any of its public methods.
you should be declare target object, and initialization, then you can use getDummy(), or you can modify getDummy() method to static .
sry, my english very bad, but i think i can help you. first, you create a public variable out of main, in you code, you only create a local variable. next, in your main, you type : "getDummy();". good luck
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.
I am confused with the static binding example below. I reckon that S2.x and S2.y shows static binding as they prints out the fields according to s2's static type. And S2.foo() makes s2call the foo method in the super class, as foo is not over ridden in the subclass.
However with S2.goo(), isn't it supposed to call the goo() method in the Test1 subclass? Like it's polymorphism? How come it's static binding? It looks like S2.goo() calls the Super Class goo()method and prints out =13. Many thanks for your help in advance!
public class SuperClass {
public int x = 10;
static int y = 10;
protected SuperClass() {
x = y++;
}
public int foo() {
return x;
}
public static int goo() {
return y;
}
}
and SubClass
public class Test1 extends SuperClass {
static int x = 15;
static int y = 15;
int x2= 20;
static int y2 = 20;
Test1()
{
x2 = y2++;
}
public int foo2() {
return x2;
}
public static int goo2() {
return y2;
}
public static int goo(){
return y2;
}
public static void main(String[] args) {
SuperClass s1 = new SuperClass();
SuperClass s2 = new Test1();
Test1 t1 = new Test1();
System.out.println("S2.x = " + s2.x);
System.out.println("S2.y = " + s2.y);
System.out.println("S2.foo() = " + s2.foo());
System.out.println("S2.goo() = " + s2.goo());
}
}
In Java static variables and methods are not polymorphic. You can't expect polymorphic behavior with static fields.
Static methods can not be overridden, because they get bind with the class at compile time itself.
However, you can hide the static behavior of a class like this:
public class Animal {
public static void foo() {
System.out.println("Animal");
}
public static void main(String[] args) {
Animal.foo(); // prints Animal
Cat.foo(); // prints Cat
}
}
class Cat extends Animal {
public static void foo() { // hides Animal.foo()
System.out.println("Cat");
}
}
Output:
Animal
Cat
Refer link for method hiding in Java.
Also, take care that static method should not be called on instance as they get bind to Class itself.
I would just like a clear example of how to instantiate a public final class in Java. I have to use a method from a class like this for a project, and have no idea how to instantiate it in the first place. Very difficult to find a clear example and explanation of the proper syntax. Thanks for the help.
public class Test {
public static void main(String[] args) {
Project pro = new Project();
pro.getName();
}
}
final class Project{
public String getName(){return "";}
}
===============================
A final class can be created like a normal class, Only thing is it can not be extended
This is an example
public class del {
public static void main(String args[])
{
x x1=new x();
System.out.println(x1.u());
}
}
final class x
{
public String u()
{
return "hi";
}
}
As you can see,x is a final class and have a method u which returns a string.
I am instatiating x in class del and calling its method u.
The output is hi
For more info click on final
final class Test{
public void callMe(){
System.out.println("In callMe method.");
}
}
public class TestingFinalClass{
public static void main(String[] args){
Test t1 = new Test();
t1.callMe();
}
}
Output : In callMe method.
final in java is applied to variable,method,class
final variable : the variable can not be signed with another value.
final method : the method cannot not be overridden.
final class : the class cannot extended.
The best example is String class in java. public final class String you can access the methods of String class as normal.
Some links
final keyword
String class
public class Test {
public static void main(String[] args) {
StdRandom stdRandom = StdRandom.getInstance(); /* this will retun an instance of the class, if needed you can use it */
int result =StdRandom.uniform(1);
System.out.println(result);
}
}
final class StdRandom{
private static StdRandom stdRandom = new StdRandom();
private StdRandom(){
}
public static StdRandom getInstance(){
return stdRandom;
}
public static int uniform(int N){
// Implement your logic here
return N;
}
}
i have a question regarding default constructors in java.
as much as i have read about constructors in java, a default constructor initializes all instance variables to their default values. but what if we define a constructor for a class, then how come variables are initialized to their default values if we want them to ?
suppose i have 2 files
a.java
public class a
{
int x;
public a(int z)
{
if(z > 0)
{
x = z;
}
}
public void get()
{
System.out.println(x);
}
}
and b.java
public class b
{
public static void main(String[] args)
{
a obj = new a(-4);
obj.get();
}
}
now here condition (z>0) fails, so x is initialized to zero. but what exactly does this as their is no default constructor in class a.
Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type.
Source
That means that the compiler will do that for you when you build the program.
In your a class (renamed A below to follow conventions) as you have written it, there is no default constructor. A default constructor for a class is a constructor which is public and has no arguments.
With the code you have written, this will fail:
A a = new A();
As soon as you declare another constructor in a class, there is no default constructor anymore.
As to instance variables, if you do not initialize them explicitly, they are set to default values. That is:
public class A
{
private int x;
public int getX()
{
return x;
}
}
if you do:
final A a = new A();
System.out.println(a.getX());
this will print out 0. The class above is exactly equivalent to:
public class A
{
private int x /* = 0 -- default value for uninitialized int instance variables */;
// redundant
public A()
{
}
public int getX()
{
return x;
}
}
When you declare a variable in java, it's by default initialized as it's default value.
For the primitive types is that 0 (or it's equivalent), for Objects is that null
So in your example has x initialy the value 0 and you never overwrite it.
In java if we dont define any constructor, it takes its self a default constructor which will have default values.while we creating object we intialize values to the constructor.
public class Car{
int numOfWheels;
public Car(int numOfWheels){ //created constructor//
this.numOfWheels=numOfwheels;
}
}
public static void main(string[]args){
Car skoda = new Car(4);
} *//initialized value to the object using constructor in parenthesis//*
public class a
{
int x;
public a(int z)
{
//as we know that every class base class is Object class this constructor first statement is super(); means compiler automatically add this statement that call object class default constructor and initialize default values
if(z > 0)
{
x = z;
}
}
public void get()
{
System.out.println(x);
}
}
public class a
{
int x;
public a(int z)
{
/*As we know that every class base class is Object.....
compiler automatically add super(); that call Object class default constructor
that initialize the value of instance variable */
if(z > 0)
{
x = z;
}
}
public void get()
{
System.out.println(x);
}
}
public class b
{
public static void main(String[] args)
{
a obj = new a(-4);
obj.get();
}
}
//other example is below
/*class A
{
public A()
{
System.out.println("Arjun Singh Rawat and Ashish Chinaliya");
}
}
class B extends A
{
public B()
{
//compiler automatically add super();
}
}
class calling
{
public static void main(String arg[])
{
B bb=new B();
}
}*/
thanks...
public class Demo {
static int a;
Demo(int b )
{
System.out.println(" Constructor");
}
public static int func()
{
System.out.println(a);
return 1;
}
public static void main( String args[])
{
new Demo(func());
}
}
Output
0
Constructor
It's not the default constructor that initializes our fields to their default values ,
In this program, the static field a is being initialized to 0 even though there is no default constructor
added by the compiler. Also it's being initialized to 0 before a constructor being called .