This is my first day learning java (on my own), so I need some help.
This is my code:
public class java_main {
public static void main(String[] args) {
MyClass my = new MyClass(3,4);
MyClass your = new MyClass();
}
public class MyClass {
public int a,b;
public Myclass(int i, int j) {
a = i;
b = j;
}
public Myclass() {
a = 1;
b = 2;
}
}
}
I'm getting this error:
No enclosing instance of type java_main is accessible. Must qualify the allocation with an enclosing instance of type java_main (e.g. x.new A() where x is an instance of java_main).
Any suggestions? Thanks in advance!!!
The problem you have is that you have enclosed in java_main class MyClass
public class java_main {
public class MyClass {
}
}
Remove the java_main, to get valid result.
public class MyClass {
public static void main(String[] args) {
MyClass my = new MyClass(3,4);
MyClass your = new MyClass();
}
private final int a,b;
public Myclass() {
this(1,2);
}
public Myclass(int a, int b) {
this.a = a;
this.b = b;
}
}
The ussue you have is casued that you have to create first instance of outer class in way to be create instance of inner.
public static void main(String[] args)
{
java_main outer = new java_main();
Myclass my = outer.new Myclass(3,4);
Myclass your = outer.new Myclass();
}
The key word static apply to parts of code that is not part of object it is only enclosed by its path (a method must be in class).
Try to find a tutorial that will guide you.
You could make MyClass public:
public static class MyClass{
...
}
It works ...
public class java_main{
public static void main(String[] args) {
// TODO Auto-generated method stub
Myclass my = new Myclass(3,4);
Myclass your = new Myclass();
System.out.println("keval");
}
}
class Myclass
{
public int a,b;
public Myclass(int i, int j)
{
a = i;
b = j;
}
public Myclass()
{
a = 1;
b = 2;
}
}
change your code to this:
public class java_main {
public static void main(String[] args)
{
Myclass my = new Myclass(3,4);
Myclass your = new Myclass();
}
}
class Myclass
{
public int a,b;
public Myclass(int i, int j)
{
a = i;
b = j;
}
public Myclass()
{
a = 1;
b = 2;
}
}
Just try this,
public class java_main {
public static void main(String[] args) {
MyClass my = new java_main().new MyClass(3, 4);
MyClass your = new java_main().new MyClass();
}
public class MyClass {
public int a, b;
public MyClass(int i, int j) {
a = i;
b = j;
}
public MyClass() {
a = 1;
b = 2;
}
}
}
Better approach is move the public class into separate file
Class name should start with Capital letter as per Java naming standard.
Here, I just created an instance of java_main and by using that instance created an instance for MyClass. This is an approach if you don't want to make MyClass as static inner class.
else make MyClass as static inner class it will work but it depends on use case,
public class java_main {
public static void main(String[] args) {
MyClass my = new java_main().new MyClass(3, 4);
MyClass your = new java_main().new MyClass();
}
public class MyClass {
public int a, b;
public MyClass(int i, int j) {
a = i;
b = j;
}
public MyClass() {
a = 1;
b = 2;
}
}
}
The problem you are having is you cannot reference non-static variables (instances of MyClass) from the static context (main menu in java_main class).
Either you change your MyClass like this,
public static class Myclass
Or take out MyClass out of the java_main class. And remove the public access modifier from the MyClass. Because you cannot have two public classes in the same file.
public class java_main {
public static void main(String[] args)
{
Myclass my = new Myclass(3,4);
Myclass your = new Myclass();
}
}
class Myclass
{
public int a,b;
public Myclass(int i, int j)
{
a = i;
b = j;
}
public Myclass()
{
a = 1;
b = 2;
}
}
Hope this helps for you or someone else.
Cheers!
Related
When I'm running the below code there is no Error
class Base {
int x_of_base_class = 10;
public Base print_x() {
System.out.printf("X of Base Class = %d\n", this.x_of_base_class);
return this;
}
public Base set_x(int x) {
x_of_base_class = x;
return this;
}
}
class Derived extends Base {
int y_of_derived_class = 89;
public Derived print_y() {
System.out.printf("Y of Derived Class = %d\n", this.y_of_derived_class);
return this;
}
public Derived print_x_y() {
print_x();
print_y();
return this;
}
public Derived set_x_y(int x, int y) {
x_of_base_class = x;
y_of_derived_class = y;
return this;
}
}
public class main_class {
public static void main(String[] args) {
Derived obj = new Derived();
obj.set_x(78).print_x();
obj.set_x_y(458, 347).print_x_y();
}
}
But when I'm running the below code with the same two classes it is giving an error
public class main_class {
public static void main(String[] args) {
Derived obj = new Derived();
obj.set_x(78).print_x_y();
}
}
And Error is generated because of obj.set_x(78).print_x_y()
So, please help me to return the object of derived class from base class
You can use generics to do this:
class Base<T extends Base> {
int x_of_base_class = 10;
public T print_x() {
System.out.printf("X of Base Class = %d\n", this.x_of_base_class);
return (T)this;
}
public T set_x(int x) {
x_of_base_class = x;
return (T)this;
}
}
class Derived extends Base<Derived> {
...
Below I have 2 classes. The class 'ran' is the main class which will call the 'MyClass' class.
Question: How can I change 'MyClass' so that I can make it work with MyClass<Integer>?
Error: in ran class because I am using <Integer>.
public class ran {
public static void main(String[] args){
MyClass<Integer> m = new MyClass<Integer>();
}
}
public class MyClass{
public MyClass(int n){}
}
the following is a simple usage of generics. T is a template class for which we can use Integer, Float etc.
public class SOJavaApplication {
public static void main(String[] args) {
MyClass<Integer> m = new MyClass<Integer>(5);
m.printScreen();
}
}
//T is the generic type
public class MyClass<T> {
T value; //attribute of type T
public MyClass(T value) {
this.value = value;
}
public void printScreen() {
System.out.println(value);
}
public void add(T n) {
value += n;
}
}
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.
class A {
}
public class B extends A {
public static void main(String[] args) {
A m = new A();
B n = (B)m;
}
}
this code can not be complied. However, in the code below, this downcast works.
class A {
}
public class B extends A implements Cloneable{
#Override
public B clone() throws CloneNotSupportedException {
return (B)super.clone();
}
public static void main(String[] args) {
B m = new B();
B n = m.clone();
}
}
so, why this downcast works?
=============Correction============================
sorry for my fault, it should be B n = **(B)**m;, not B n = m;.
I'm very sorry. I have corrected it in the above code.
Even in first case -;
class A {
}
public class B extends A {
public static void main(String[] args) {
A m = new A();
// B n = m;
B n = (B)m;
}
}
It's work.
WHAT?
You cannot cast A to B no mather what you people are saying
IF A extends B than B can be threated as insance of A and B but A cannot be instance of B.
It is possiable to instantiate child object from parent class without child class name.
For example, I have next classes:
public class A {
protected int a;
public A1() {
a = 0;
}
public int getA() {
return a;
}
public static A getObject() {
// some code which I need to write
}
}
public class A1 extends A {
public A1() {
a = 5;
}
}
public class A2 extends A {
public A2() {
a = 10;
}
}
Usage example:
A a = A1.getObject();
a.getA(); // return 5
a = A2.getObject();
a.getA(); // return 10
a = A.getObject();
a.getA(); // return 0
A1, A2 it is not all child classes. There are may be unlimited numbers.
How can I write getObject() method to it creates A class childs instanses.
PS:
I just need to initialize child class object, but there are large amounts of child classes and I would not to call "new" for all of them and I would not to write static method to initialize it, also, all childs have same constructors. It is big lack that I can't create child instance from parent.
When you write A a = A1.getObject(),
you do use child classname (A1).
So a) your question is misleading and
b) why can't you just write A a = new A1()?
If you want to write A1.getObject(), then you can redefine getObject() in class A1:
public class A1 extends A {
public static A getObject() {
return new A1();
}
}
Without redefining, there is no way to declare getObject() in class A so that it return objects different classes because A a = A1.getObject() would compile to A a = A.getObject().
class A
{
protected int a;
public A()
{
a = 0;
}
public int getA()
{
return a;
}
public static A getObject(Class c) throws Exception
{
A obj = (A)c.newInstance();
return obj;
}
}
class A1 extends A
{
public A1()
{
a = 5;
}
}
class A2 extends A
{
public A2()
{
a = 10;
}
}
class Test{
public static void main(String args[]) throws Exception
{
A a = A1.getObject(A1.class);
System.out.println(a.getA()); // return 5
a = A2.getObject(A2.class);
System.out.println(a.getA()); // return 10
a = A.getObject(A.class);
System.out.println(a.getA()); // return 0
}
}