class A {
int a, b;
A(int i, int j) {
a = i;
b = j;
}
}
class B extends A {
int c, d;
B(int i, int j) {
c = i;
d = j;
}
}
public class HelloWorld {
public static void main(String[] args) {
A aa = new A(5, 6);
B bb = new B(3, 4);
System.out.println(aa.a + aa.b + bb.a + bb.b + bb.c + bb.d);
}
}
it gives error as
HelloWorld.java:9: error: constructor A in class A cannot be applied to given types;
{
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
As you class B extends class A, you somewhere have to call the constructor of the class you are extending.
As your class A does not have a constructor with no arguments, you need to explicitly call the super constructor (the constructor from class A) as the first statement inside the constructor of your class B:
B(int i, int j) {
super(i, j);
// Your code
}
If you would have a no-args constructor in A, you would not need to do this, as the no-args constructor is implicitly called if no constructor call is specified:
B(int i, int j) {
// Your code
}
Is actually doing:
B() {
super();
// Your code
}
And as you do not have A() as a constructor, you get the error.
Check this one...
JVM always looking for no argument constructor.
class A {
int a, b;
A(int i, int j) {
a = i;
b = j;
}
public A() {
// TODO Auto-generated constructor stub
}
}
class B extends A {
int c, d;
B(int i, int j) {
c = i;
d = j;
}
}
public class pivot {
public static void main(String[] args) {
A aa = new A(5, 6);
B bb = new B(3, 4);
System.out.println(aa.a + aa.b + bb.a + bb.b + bb.c + bb.d);
}
}
Related
I am writing code in Java which has multiple methods and these methods have multiple variables. I want the other methods to access the variables of another method using actual and formal parameters. How can I do it?
I am pasting an example of the problem I'm facing.
Error : variable is not defined.
Code
public class example {
public void addition() {
int a = 0;
int b = 10;
int c = a + b;
}
public void result() {
System.out.println("The result for the above addition is" + c);
}
}
IM GETTING AN ERROR SAYING VARIABLE IS NOT DEFINED
You should declare c as global variable
public class Example {
int c;
public void addition() {
int a = 0;
int b = 10;
c = a + b;
}
public void result() {
System.out.println("The result for the above addition is " + c);
}
public static void main(String[] args) {
Example e = new Example();
e.addition();
e.result();
}
}
well, your java syntax is quite wrong... if you need to do an addition, you can do as follows:
public class Addition {
public static int addition(int a, int b)
{
int c= a + b;
return c;
}
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = addition(a,b);
System.out.println("The result for the above addition is " + c);
}
}
where addition function does add a + b and return the result to your main method.
Why the output is "021"? Why there are "0" and "1"(since "i" get "2" why it changes to "1")?
public class C {
protected int i;
public C(int i){
this(i,i);
System.out.print(this.i);
this.i=i;
}
public C(int i, int j) {
System.out.print(this.i);
this.i=i+j;
}
public C(){
this(1);
System.out.print(i);
}
public static void main(String[] args) {
C c=new C();
}}
C() calls C(1) which calls C(1,1)
C(1,1) prints 0 (the default value of this.i) and assigns 2 (i+j) to this.i
then C(1) prints 2 and assigns 1 to this.i
then C() prints 1
I think this is better for understanding:
public C(int i) {
this(i, i);
System.out.println("*"+this.i);
this.i = i;
}
public C(int i, int j) {
System.out.println("#"+this.i);
this.i = i + j;
}
public C() {
this(1);
System.out.println("#"+i);
}
Now, you can get the sequence of these methods when you invoke C();
Here the code commented, you will understand your problem now,
public class C {
protected int i;
public C(int i) {
this(i, i); // got to two parameter constructer and after the result print the next line
System.out.print(" + second "+this.i); // print value of i which is come from C(int i, int j) = 2
this.i = i; // set the value of i to 1
}
public C(int i, int j) {
System.out.print("first "+this.i); // print the value of i (in this case 0 the default value)
this.i = i + j; // set i to 2
}
public C() {
this(1); // got to one parameter constructer and after the result print the next line
System.out.print(" + Third is "+i); // print value of i which is come from C(int i) = 1
}
public static void main(String[] args) {
C c = new C();
}
}
I hope that help.
I have methods in a class
public class ReflectionClass {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public String concatenate (String a, String b, String c){
return a + b + c;
}
}
I'm trying to call these methods through reflection. All I have in hand are - the method name and the parameters. Is there a way to pass the parameters into the Method.Invoke() method dynamically based on the number/type of parameters I have in hand?
As you can see in the docs public Object invoke(Object obj, Object... args) takes a varargs argument - so you can simply pass an array of arguments there.
You need to create an instance, get the methods, get the parameters, check the parameters by checking the type and how many... then call invoke depending of what
Example:
public static void main(String[] args) throws Exception {
Class<?> cls = Class.forName("com.ReflectionClass");
Object obj = cls.newInstance();
for (Method m : cls.getDeclaredMethods()) {
if (m.getParameterCount() == 3 && Arrays.asList(m.getParameterTypes()).contains(String.class)) {
String a = "A";
String b = "B";
String c = "C";
Object returnVal = m.invoke(obj, a, b, c);
System.out.println((String) returnVal);
} else if (m.getParameterCount() == 2 && Arrays.asList(m.getParameterTypes()).contains(int.class)) {
int a = 5;
int b = 3;
Object returnVal = m.invoke(obj, a, b);
System.out.println(returnVal);
} else if (m.getParameterCount() == 3 && Arrays.asList(m.getParameterTypes()).contains(int.class)) {
int a = 5;
int b = 3;
int c = 3;
Object returnVal = m.invoke(obj, a, b, c);
System.out.println(returnVal);
}
}
}
This code has a compilation problem. How do I get a.f()?
class A {
int i = 1;
int f() {
return i;
}
static char g() {
return 'A';
}
}
class B extends A {
int i = 2;
int f() {
return -i;
}
static char g() {
return 'B';
}
}
class Test {
public static void main(String args[]) {
B b = new B();
System.out.println(b.i);
System.out.println(b.f());
System.out.println(b.g());
System.out.println(B.g());
A a = b;
System.out.println(a.i);
System.out.println(a.f());
System.out.println(a.g());
System.out.println(A.g());
}
}
and this is the result
2
-2
B
B
1
-2
A
A
You've not specified any access modifiers for your class members, so it has default or package level access only and is not available to sub classes
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
See here for more details
If you change the signature of f() to protected int f() then it should work
Of course you have the same issue with the other members of the class so it's up to you how you control this
Overriding is just for instance methods, not for variables. So, when you get the variable i from instance of the class A, you get the value from this Class. The same thing with the instance of class B.
But you can reinitialize the value of i in constructor, for example:
class B extends A {
public B() {
i=2;
}
int f() {
return -i;
}
static char g() {
return 'B';
}
I was trying to make a program that searches for a random number, but i had problems importing the "a" variable in the other method. I would be happy if i could get some explanation. I have already tried to make the variable static, but that doesn't work
import java.util.Random;
public class verschlüsselung {
private static void nummber(int a) {
Random r = new Random();
a = r.nextInt(999);
System.out.println(a);
}
private static void search(int b) {
b = 0;
if(b =! a) {
for(b = 1; b =! a ; b++) {
if(b == a) {
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
nummber(0);
search(0);
}
}
There is no such thing as using local variables in other methods.
You can return the variable from one method. Than call this method from other and get there the variable.
Declare the variable 'a' to be static and remove the parameter 'a' passed in the nummber()
function. This function does not need any input as it assigns the value of a random number to the global static variable 'a' which is accessed in the method search().
your declaration and method signature should read :
private static int a;
private static void nummber(){....}
May this help you:
private static int nummber( int a){
Random r = new Random();
a =r.nextInt(999);
System.out.println(a);
return a;
}
private static void search(int b, int a){
b = 0;
if(b =! a){
for(b =1; b =! a ; b++){
if(b == a){
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
int a = nummber(0);
search(0, a);
}