interface Interf {
void m1();
}
public class Test {
int x = 888;
Interf f;
public void m2() {
Integer x = 777;
f = new Interf() {
Integer x = 666;
public void m1() {
int x = 555;
System.out.println(x);
System.out.println(this.x);
}
};
}
public static void main(String[] args) {
Test t = new Test();
t.m2();
t.f.m1();
}
}
How can I access x variable with value 777 inside m1() method(In Anonymous class) with same name? Is it possible to access?
No, because it is shadowed. But you can change the name (and no need for Integer, int will suffice).
public void m2() {
int y = 777;
f = new Interf() {
int x = 666;
public void m1() {
int x = 555;
System.out.println(x);
System.out.println(this.x);
System.out.println(y);
}
};
}
Outputs
555
666
777
Related
I recived some tasks as homework after I did just 1h of course about interfaces, did them but this gave me real problems:
An interface I1 having: method: int read();
A class A implements the interface and containing: the method int read() that read a value from the keyboard;
A class B containing:a variable c type I1, a constructor with one parameter for assigning value for c, a method afis() that display the summ of the numbers.
This is what I did so far, but I'm stuck here, how can i assign a value to c and add it with the n1 from A class?
package toDo2;
import javax.swing.JOptionPane;
public interface i1 {
int read();
}
class A implements i1 {
public int read() {
int n1 = Integer.parseInt(JOptionPane.showInputDialog("Introduce the value for n1"));
return n1;
}
}
class B {
int sum;
i1 c;
B(i1 c) {
}
}
class toDo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
B n1 = new B(/*here must be the value of c which is i1 type*/);
}
}
It appears, you need to create an instance of class A in your main method because it is so far the only class implementing interface i1:
class B {
int sum;
i1 c;
B(i1 c) {
this.c = c;
}
}
class toDo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
B n1 = new B(new A());
// ...
}
}
Also, class B won't be aware of specific implementation of its c field - it will be able to invoke method int read() defined in your interface.
This will do
B n1 = new B(new A());
Basically your class A is of type I1 because it's implementing I1.
Here you go:
import java.util.Scanner;
public class Main{
public static void main(String []args){
I1 a = new A();
a.read(); //this call will initiate taking input
B n1 = new B(a);
int sum = n1.sum(a.getFirstNumber(), a.getSecondNumber());
System.out.println(sum);
}
}
interface I1 {
void read();
int getFirstNumber();
int getSecondNumber();
}
class A implements I1 {
int n1;
int n2;
public void read() {
Scanner in = new Scanner(System.in);
n1 = in.nextInt();
n2 = in.nextInt();
}
public int getFirstNumber() {
return n1;
}
public int getSecondNumber() {
return n2;
}
}
class B {
I1 c;
B(I1 c) {
this.c = c;
}
public int sum(int n1, int n2) {
return n1+n2;
}
}
I'm working with Java. I have a class with 2 constructors. The first constructor takes an int value as a parameter and sets an int variable as that value. The second constructor takes a string and prints it out. The idea is that when I call the first constructor from my main class, it sets an integer value. And when I call the second constructor in the main class, it takes the string representation of int variable of the first constructor and prints it out.
Here's how I made the constructors:
public class Test
{
int val;
public Test(int x)
{
val = x;
return val; //I know this won't work. So I'm looking for an alternative
}
public Test(String y)
{
System.out.println("The value is " + y);
}
}
And the main method (in a different class) looks like this:
public static void main(String [] args)
{
Test t1 = new Test(6);
Test t2 = new Test(String.valueOf(t1)); //This won't work because the first constructor can't return a value
}
So how exactly can I change the contents of the constructors so that I can pass val into the 2nd constructor?
Override toString() to return value so when you so String.valueOf(t1) it will do the toString() method;
public class Test
{
int val;
public Test(int x)
{
val = x;
}
public Test(String y)
{
System.out.println("The value is " + y);
}
#Override
public String toString()
{
return String.valueOf(val);
}
}
I think what you are probably actually trying to do is to override the toString() method of Test.
public class Test
{
int val;
public Test(int x)
{
val = x;
}
#Override
public String toString() {
return "Test:"+val;
}
}
Then you can do this:
public static void main(String [] args)
{
Test t1 = new Test(6);
String s = t1.toString();
// or this
System.out.println( t1 ); // prints "Test: 6"
}
What you're describing is actually impossible without some changes.
First and foremost, t1 and t2 are two separate instances and the values inside of them have no bearing on one another. So t1 has x=6 and t2 has x=0 (because of default values).
If you want your second constructor to have a value of x that isn't 0, then you'll need to pass that in too.
public Test(int x, String s) {
super(x);
System.out.println(x);
}
I think you don't really want two constructors. It seems like you're wanting to do something like the following:
public class Test
{
int val;
public Test(int x)
{
val = x;
}
public void printVal()
{
System.out.println("The value is " + val);
}
public static void main(String [] args)
{
Test t1 = new Test(6);
t1.printVal();
}
}
Your requirement is kinda weird. But this will work even it is kinda weird
public class Test {
private static int val;
public Test(int x) {
val = x;
}
public Test() {
System.out.println("The value is " + String.valueOf(val));
}
public static void main(String[] args) {
Test t1 = new Test(6);
Test t2 = new Test();
}
}
Could some one tell me how can i get variable inside methods and the reverse.
Something like:
i want to use variable y inside that method func, and get that x from that method func and use it inside main.
class test{
int y = 4;
void func(){
int x = 3;
}
public static void main(String[] args)
{
// take x inside main
}}
You can always use class variable inside methods. To use x of func() inside main() method, you can return it from func() or save it into some class variable
class TestClass {
int y = 4;
int x = 0;
//func returning x
int func1() {
int x = y;
return x;
}
//func storing it to class variable
void func2() {
this.x = 3;
}
public static void main(String[] args) {
TestClass t = new TestClass();
int xOfFunc = t.func1();
t.func2();
System.out.println("x Of Func :: " + xOfFunc + "\n class variable x :: " + t.x);
}
}
output :
x Of Func :: 4
class variable x :: 3
class test{
int y = 4;
int func(){
int x = 3;
return x;
}
public static void main(String[] args)
{
test obj = new test();
int x = obj.func();
}
}
or you can make func() method static and you will be able to call this method without creating an object of class:
class test{
int y = 4;
static int func(){
int x = 3;
return x;
}
public static void main(String[] args)
{
int x = func();
}
}
Try something like this:
class Main {
public int y= 4;
int func(){
return 4;
}
public static void main(String... args){
Main m = new Main();
int x = m.func();
int y = m.y;
}
}
class test{
int y = 4;
int x;
void func(){
int x = 3;
this.x = 3; //make it usable from the class
}
}
y should be accessible inside the function. If the function uses a variable y by itself you can use this.y to access the variable.
Make it static like this allows you to access it everywhere by calling test.y.
class test{
public static int y = 4;
void func(){
int x = 3;
}
}
Then you can do this in main.
public static void main(String[] args)
{
int value = test.y;
}
I have this 2 classes:
public class A {
protected int _x;
public A() {
_x = 1;
}
public A(int x) {
_x = x;
}
public void f(int x) {
_x += x;
}
public String toString() {
return "" + _x;
}
}
public class B extends A {
public B() {
super(3);
}
public B(int x) {
super.f(x);
f(x);
}
public void f(int x) {
_x -= x;
super.f(x);
}
public static void main(String[] args) {
A[] arr = new A[3];
arr[0] = new B();
arr[1] = new A();
arr[2] = new B(5);
for (int i = 0; i < arr.length; i++) {
arr[i].f(2);
System.out.print(arr[i] + " ");
}
}
}
The output is 3 3 6 and I am wonder why the third iteration is 6
The constructor:
public B(int x)
{
super.f(x);
f(x);
}
is translated by the compiler to this:
public B(int x)
{
super();
super.f(x);
f(x);
}
I guess now you would understand, why it's 6.
I'm just beginning in programming and I'd like to make exercise from a book, but I can't. That's my problem:
public class increment {
int increment() {
return this + 1; // aka this++
}
public static void main(String[] args) {
int a = 0;
System.out.println(a.increment());
}
}
As you for sure guessed already, that it doesn't works, I want to ask you how to get outputed integer a incremented by one, but using keyword 'this'.
Regards and sorry for stupid questions.
It is strange to name a class like a method.
I guess you wanted this:
public class Counter {
int val;
public Counter (int start) {
val = start;
}
public void increment() {
val ++;
}
public String toString () {
return Integer.toString (val);
}
public static void main(String[] args) {
Counter counter = new Counter (0);
counter.increment ();
System.out.println(counter.toString ());
}
}
this is an object (the current object). You cannot "increment" it.
A way to do it is:
public class Increment {
int a = 0;
int increment() {
return a + 1;
// or: return this.a + 1;
// or: a++; return a; if you want a to be incremented from now on
}
public static void main(String[] args) {
Increment inc = new Increment();
System.out.println(inc.increment());
}
}
The this keyword in Java refers to the current scope's object instance. I don't think it's what you're looking for in this case.
In your example, a isn't an object of the class increment, it is a primitive int. In order to use the .increment() function you defined, it would have to be an object of type increment.
One option that may be what you're looking for would be the following.
public class Increment { //Java likes capitalized class names
private int myInt;
public Increment(int a) { //constructor
myInt = a;
}
public int increment() {
return ++myInt;
}
public static void main(String[] args) {
Increment a = new Increment(0);
System.out.println(a.increment());
}
}
In this example, we make a new class of type increment, which internally contains an integer. Its increment method increments that internal integer, and then returns the number.
you are using operator + for your current object (this). Operator overloading is not supported in java.
Something like this will work:
class MyInteger {
private int internal;
public MyInteger( int value ){
this.internal = value;
}
public int incerment(){
return ++this.internal;
}
}
public class Increment {
public static void main(String[] args) {
MyInteger a = new MyInteger(0);
System.out.println(a.increment());
}
}
You see, you can only implement methods for your own classes, not for existing classes, or for primitives like int.
i don't think you can use this to return the value, except if you're making a new class like this:
class Increment1
{
private int a;
public int increment2(int a)
{
this.a=a;
return this.a + 1;
}
}
public class Increment
{
static Increment1 b = new Increment1();
public static void main(String[] args)
{
int a = 0;
System.out.println(b.increment2(a));
}
}
You cannot increment a class like this.
You have to use a member variable that you can increment.
public class Test {
private int var;
public Test(int i) {
this.var = i;
}
int increment() {
this.var++;
}
}
public static void main(String[] args) {
Test t = new Test(0);
System.out.println(t.increment());
}
This refers to the current instance of the class, not a particular member.
You want to increment a property (I'm guessing of type long or int), and not the instance of your increment class (should be Increment, by the way).
Something like this would work:
public class increment {
private int innerValue = 0;
int increment() {
innerValue+=1
return innerValue; // aka this++
}
public static void main(String[] args) {
increment a = new increment()
System.out.println(a.increment());
}
}