Whats wrong with the given Java Code [duplicate] - java

This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 7 years ago.
Code is
public class ctorsandobjs {
private int a;
public int b;
public ctorsandobjs(String arg)
{
System.out.println("I got " + arg);
}
public void add(int a,int b)
{
System.out.println("Addition is " + String.valueOf(a+b));
}
public static void main(String args[])
{
ctorsandobjs c = new ctorsandobjs("You");
c.a = 12;
c.b = 15;
add(c.a,c.b); //compiler shows error here
}
}
I am using Eclipse Luna IDE and JDK 8 ...
can you tell me why compiler is showing error here.....
"Cannot make a static reference to a non static method add(int,int) from the type ctorsandobjs"
I am new to JAVA...
and if possible suggest a solution

add is a non-static method and so you have to invoke it from the object of a class
You have to do:
c.add(c.a, c.b);

You cannot reference non-static members (private int a; public int b) from within a static function.

The add method is not a static method, so you need to call it on an instance of the class ctorsandobjs, for example like this:
c.add(c.a,c.b);

Related

Using of methods instead of constructor [duplicate]

This question already has answers here:
Purpose of a constructor in Java?
(12 answers)
Closed 2 years ago.
When we can use methods instead of constructor for any operation then what is the use of constructor in java or c++.
//Program of Division using constructor:-
class Hey {
Hey() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main ( String[] args ) {
Hey ob = new Hey();
}
}
//Program of division using method:-
class Hey {
public void disp() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main( String[] args ) {
Hey ob = new Hey();
ob.disp();
}
}
As, we can see that both will have same output. So, now I am bit confuse that when to use constructor.
Constructor is used to initialize objects in java. Even if you don't provide constructor in your code, java compiler will automatically add a default constructor.
Whereas Methods are used to exhibits functionalities to object. You will have to invoke methods explicitly in your code.
In the example you shared, you are creating object of Hey class Hey ob=new Hey() in order to call its method disp. So if you want to define object in your class, you will use constructors, and if you want to write some functionality of object, you can use Methods.

Scope of statics and key word "this" in java [duplicate]

This question already has answers here:
Why can't we use 'this' keyword in a static method
(9 answers)
Closed 5 years ago.
I have a question about this fragment of code :
public class Inner {
static int a;
public static void main(String[] args) {
a = 0;
}
public static void g() {
this.a = 0;
}
}
`
Why we can't use "this.a" in static method, but we can use "a" without "this"?
Photo of compilation error: https://www.dropbox.com/s/5q6y3ldsf37p0h3/%D0%97%D0%BD%D1%96%D0%BC%D0%BE%D0%BA%20%D0%B5%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202017-05-27%2017.28.34.png?dl=0
Because this points to an instance of the class, in the static method you don't have an instance.
The this keyword refers to the current instance of the class. Static member functions do not have a this pointer
You'll notice the definition of a static member is
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object
Which is why this has nothing to point to.

Does static variable inherited? [duplicate]

This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
We know that in java static variable are not inherited. But in below code I am not getting any error as I want to initialize the static variable in child class.
class s
{
static int x;
}
class aaa extends s
{
void fun()
{
x=2;
System.out.println(x);
}
public static void main(String args[])
{
aaa w=new aaa();
w.fun();
}
}
static members are most definitely accessible from subclasses, as your example shows. You cannot override them, of course, but you could hide them.

Java static main also codingBat

public class CodingBat {
public static void main(String[] args) {
System.out.println(sumDouble(5,5));
}
public int sumDouble(int a, int b) {
if( a ==b) {
return 2*a + 2* b;
} else{
return a + b;
}
}
}
So I made this code, and I'm really confused why it doesn't work unless I write static between the public int sumDouble, because I was practicing on codingBat, and to answer the question they did not involve static, but then how do they test code. Do they use the main? I mean you have to to get the code running right?
So to my knowledge, static means that every object of this class will all contain the same value.But I don't see the relevance of this error.
"Cannot make a static reference to the non-static method"
Thank you for your help :D
and I'm really confused why it doesn't work unless I write static
between the public int sumDouble,
Yes, static is required
Since the main method is static and the sumDouble() method is not, you can't call the method without creating object of class. You cannot refer non-static members from a static method.
Either make method static or create object as below and then access method.
CodingBat obj = new CodingBat();
System.out.println(obj.sumDouble(5,5));
Refer here for more
Either you call it through a static context, meaning like you do (or, from another class, by: ClassName.methodName(); )
Or, you have to call it as an instance method, which it is, if you don't declare it static.Then, however, you'll need an instance to call it through:
public static void main(String[] args){
CodingBat cB = new CodingBat();
System.out.println(cB.sumDouble(5,5));
}
You need to create an object in order to use this method
public class CodingBat {
public static void main(String[] args){
CodingBat obj = new CodingBat();
System.out.println(obj.sumDouble(5,5));
}
public int sumDouble(int a, int b) {
if( a ==b){
return 2*a + 2* b;}
else{
return a + b;}
}
}

error when calling a void function [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Cannot make a static reference to the non-static method
(8 answers)
Closed 8 years ago.
public class Makakiesmarkou {
void swap(int i, int j, int[] arr) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public void MySort(int[] T)
{
for(int m=0; m<T.length-1; m++)
{
int j=m;
for(int k=m+1; m<T.length-1; k++)
{
if(T[k]<T[j])
j=k;
}
swap(T[j], T[m], T);
}
}
public static void main(String[] args) {
int[] pin= new int[50];
MySort(pin);
System.out.println(Arrays.toString(pin));
}
}
the error when i call MySort in the main class is "non static method MySort[int[]] cannot be referenced from a static context"
what am i doing wrong?
You can either do what Salah said, or you can instantiate your class and call MySort on that:
public static void main(String[] args) {
int[] pin= new int[50];
Makakiesmarkou m = new Makakiesmarkou();
m.MySort(pin);
System.out.println(Arrays.toString(pin));
}
You need to change the declaration of your method to be static like:
public static void MySort(int[] T)
static variable initialized when class is loaded into JVM on the other hand instance variable has different value for each instances and they get created when instance of an object is created either by using new() operator or using reflection like Class.newInstance().
So if you try to access a non static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until an instance is created and they are associated with any instance. So in my opinion only reason which make sense to disallow non static or instance variable inside static context is non existence of instance.
Read more here

Categories