The seem not to be able to compile the following main method:
public class MainMethod {
public static void main(String []args){
InnerizationClass outer = new InnerizationClass();
InnerizationClass.StaticInnerClass inner = outer.StaticInnerClass(); //#1
System.out.println(inner.getOuterClassVar());
System.out.println(new InnerizationClass().locInnrMeth());
}
}
And this is the InnerizationClass class:
public class InnerizationClass {
String outerClassVar = "Outer Catism";
static String outerClassVarStat = "Outer Static Catism";
public static class StaticInnerClass{
String innerClassVar = "Catism";
public String getInnerClassVar(){
return this.innerClassVar;
}
public String getOuterClassVar(){
return InnerizationClass.outerClassVarStat;
}
}
public String locInnrMeth(){
class MethodLocalInnerClass{
String methodLocalInrCls = "Method Local Catism";
void printInner(){
System.out.println(InnerizationClass.this.outerClassVar);
}
}
return new MethodLocalInnerClass().methodLocalInrCls;
}
public String getStaticOuterVar(){return null;}
}
I am getting the following error from #1 from the main method:
MainMethod.java:6 qualified new of static class
InnerizationClass.StaticInnerClass inner = outer.new StaticInnerClass();
1 error;
Try this instead:
public class MainMethod {
public static void main(String []args){
InnerizationClass.StaticInnerClass inner = new InnerizationClass.StaticInnerClass(); //#1
System.out.println(inner.getOuterClassVar());
System.out.println(new InnerizationClass().locInnrMeth());
}
}
since your inner class is static you don't need to create an instance of InnerizationClass
Related
In a small example, I'm trying to take information from the String "helloFromMain" in a file called Main.java and move it "outside" the public static void into a public static string in a different file named data.java.
IN THE FILE MAIN.JAVA
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = helloFromMain;
}
Any help would be appreciated.
Also im relatively new to all this
You can set a public static variable from another class.
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
Data.helloFromData = helloFromMain;
}
}
Also, I've found that it's helpful to set a package for all classes, as it makes it simpler to manage importing and FS structure.
You can do it like this
Simple way.
IN THE FILE MAIN.JAVA
public class Main {
public static String helloFromMain; //declare a public static string, it will be accessible from outside the class.
public static void main(String[] args){
helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = Main.helloFromMain;
}
But be aware that Main.helloFromMain will be null until you call the main constructor.
Advance way :
IN THE FILE MAIN.JAVA
public class Main {
private static String helloFromMain; //This time the static variable is private, so you can't directly use it from outside
public static void main(String[] args){
helloFromMain = "hello";
}
//We create a public static method to access the private static variable
public static String getHelloFromMain(){
return helloFromMain;
}
}
IN THE FILE DATA.JAVA
public class Data {
//We call our public static method from Main.
public static String helloFromData = Main.getHelloFromMain();
}
You can create a static method in class Data to set the value of the string. If you do this, you don't need to make the variable in class Data public.
public class Main {
public static void main(String[] args) {
String helloFromMain = "hello";
Data.setHelloFromData(helloFromMain);
}
}
public class Data {
private static String helloFromData;
public static void setHelloFromData(String newValue) {
helloFromData = newValue;
}
}
Need to call a private method foo() of the class Inner.Private, where Private is an inner private class from the main method of the main class.
The code is something like this:
public class MainClass {
public static void main(String[] args) throws Exception {
// Need to invoke foo() from here
}
static class Inner {
private class Private {
private String foo() {
return "someString";
}
}
}
}
I was trying to get this using Java Reflection, but I am facing issues from this approach.
My attempt to invoke the foo() is:
Inner innerClassObject = new Inner();
Method method = Inner.Private.class.getDeclaredMethod("foo");
method.setAccessible(true);
method.invoke(innerClassObject);
But this gives a NoSuchMethodException:
Exception in thread "main" java.lang.NoSuchMethodException:
default.MainClass$Inner$Private.foo()
at java.lang.Class.getDeclaredMethod(Unknown Source)
I am stuck at this point, is this achievable by Java Reflection, or any other way?
Ummm... why not simply new Inner().new Private().foo()?
Why are you doing this
Inner.Private.class
Instead of
innerClassObject.getClass()
For e.x:
public class Test {
private int foo(){
System.out.println("Test");
return 1;
}
public static void main(String [] args) throws InterruptedException,
NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
Test innerClassObject = new Test();
Method method =
innerClassObject.getClass().getDeclaredMethod("foo",null);
method.setAccessible(true);
method.invoke(innerClassObject);
}
}
Why waste an instantiation just to call a method as was described? You will inevitably want to save various instances for latter use as the classes develop.
public class MainClass {
public static void main(String[] args) throws Exception {
// Need to invoke foo() from here
Inner inner = new Inner();
Inner.Private pvt = inner.new Private();
System.out.println(pvt.foo());
}
static class Inner {
private class Private {
private String foo() {
return "someString";
}
}
}
}
Prints
someString
I have deal with one problem while accessing arraylist element in another class. I have 2 classes: class A and class B.
class A {
private ArrayList<String> temp=new ArrayList<String>();
temp.add("abc");
temp.add("XYZ");
public ArrayList<String> getTemp() {
return this.temp;
}
}
public class B
{
private A a=null;
public b(A aa)
{
this.a = aa;
}
System.out.printLn(a.getTemp.size());//output is 2
System.out.printLn(a.getTemp.get(0));//null
}
Why it is giving me null? Please give brief explanation of this.
Here is a working version of what you are trying to achieve:
A.java
In the A class, you should be adding elements to your ArrayList in the constructor:
public class A {
private ArrayList<String> temp=new ArrayList<String>();
public A() {
temp.add("abc");
temp.add("XYZ");
}
public ArrayList<String> getTemp() {
return this.temp;
}
}
B.java
The constructor name should match the class's:
public class B {
private A a=null;
public B(A aa)
{
this.a = aa;
}
}
App.java
public class App {
public static void main(String[] args) {
A a = new A();
System.out.println(a.getTemp().size());
System.out.println(a.getTemp().get(0));
}
}
Output:
2
abc
Your current code won't even compile.
Furthermore, I can guarantee 100% that if by some magic your code were to compile the output of the first printLn would in no way be 2. It would be null. `
**First Of All Your Code Is Not Impossible to run**
You Can't assign value to instance variable directly in side of class without constructor or method so your modified class A must be like
**A.java**
class A {
private ArrayList<String> temp=new ArrayList<String>();
public A()
{
temp.add("abc");
temp.add("XYZ");
}
public ArrayList<String> getTemp()
{
return this.temp;
}
}
OR Like
class A {
private ArrayList<String> temp=new ArrayList<String>();
public A()
{
initialize();
}
public void initialize()
{
temp.add("abc");
temp.add("XYZ");
}
public ArrayList<String> getTemp()
{
return this.temp;
}
}
And Then As per Above Your Class B will Be
**B.java**
class B
{
private A a=null;
public B(A aa)
{
this.a = aa;
}
}
And Then you have to go for main method like
**Temp.java**
public class Temp {
public static void main(String... args)
{
A a = new A();
B b = new B(a);
System.out.println(a.getTemp().size());//output is 2
System.out.println(a.getTemp().get(0));//abc
}
}
How can i get the value of string from class a to class b ?
public class A{
public String string = "A";
}
public class B{
public static void main(String []args){
System.out.printl(string);
}
}
You have to instantiate the class A and access the instance variable. Like this:
public class A{
public String string = "A";
}
public class B{
public static void main(String []args){
A a = new A();
System.out.println(a.string);
}
}
string is an instance variable of class A.
Firstly, you cannot have two public classes in the same file.
So I suggest that you move class A to A.java and leave theclass B in B.java
A.java
public class A{
public String string = "A";
}
B.java
public class B{
public static void main(String []args){
A a = new A();
System.out.println(a.string);
}
}
Upon invoking class B, a new object of class A would be create and the variable of A can be access through it.
public access to member fields cause so many problems, fix them before you have them with a technique of hiding them behind a method.
public class A {
public String getString() {
return "A";
}
}
public class B{
public static void main(String []args){
A a = new A();
System.out.println(a.getString());
}
}
I am used to code in C++, but have to convert a project from C++ to Java. In C++ using data structure is pretty much simple. I am trying to replicate the same thing, but such as a Java inner class and static nested class. After reading several examples online, and trying different versions, so far this is what I got:
public class Main {
public static void main( String[] args ) {
...
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
}
}
class ClassOuter{
public static class DataInner{
public int x;
}
...
protected void getNo()
{ value.x=Integer.parseInt("493");
}
}
However, when I try to compile, it gives me the error:
$ javac -cp "./" Main.java
Main.java:15: error: '(' expected
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
Any clue about what is missing here?
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
This syntax applies to inner classes (i.e. non static nested classes). If that's what you want, remove the static keyword from public static class DataInner.
EDIT :
Also change
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
to
ClassOuter.DataInner value = outerObj.new DataInner();
You don't specify the outer type when using an enclosing instance to initialize the inner instance.
And the line outerObj.value.x=Integer.parseInt("493"); is not valid inside your outer class's getNo() method, since outerObj and value are local variables known only to your main method.
If you wish your outer instance to update any of its inner instances, it must obtain a reference to it. Here's one way to do it :
public class Main {
public static void main( String[] args ) {
...
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = outerObj.new DataInner();
outerObj.setInner (value);
}
}
class ClassOuter{
public static class DataInner{
public int x;
}
...
private DataInner inner = null;
public void setInner (DataInner inner) {
this.inner = inner;
}
protected void getNo()
{
inner.x=Integer.parseInt("493");
}
}
If DataInner must be static class:
public class Main {
public static void main(String[] args) {
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = new ClassOuter.DataInner();
}
}
class ClassOuter {
public static class DataInner {
public int value;
}
}
It this case, DataInner has no reference to the ClassOuter instance.
If DataInner must not be static class.
public class Main {
public static void main(String[] args) {
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = outerObj.newInner();
}
}
class ClassOuter {
public class DataInner {
public int value;
}
public DataInner newInner() {
return new DataInner();
}
In this case, DataInner has reference to the ClassOuter instance (ClassOuter.this).