I have two packages; pack1 and pack2.
in pack1 I have two classes the main called Prog and another one called ClassA.
In pack2 I have one class called ClassB.
I am trying to understand why I can't call a method from ClassB using the object.
I can do that using the main class but not with another class.
Here's the code:
package pack1;
import pack2.ClassB;
public class Prog {
public static void main(String[] args){
}
}
Code for ClassA
package pack1;
import pack2.ClassB;
public class ClassA {
ClassB o3 = new ClassB();
// Error won't compile
System.out.println(o3.getText());
}
Code for ClassB:
package pack2;
public class ClassB {
final String TEXT = "This is a text";
public String getText(){
return TEXT;
}
}
The problem here isn't that you can't access the method. The problem is that statements must be enclosed either in a constructor, amethod-declaration or an initializer-block. So this would be valid code for example:
enter codepackage pack1;
import pack2.ClassB;
public class ClassA {
ClassB o3 = new ClassB();
public void someMethod(){
System.out.println(o3.getText());
}
}
//pack1 code here
package pack1;
import pack2.ClassB;
class ClassA {
}
public class Prog {
public static void main(String[] args) {
// write your code here
ClassB o3 = new ClassB();
// Error won't compile
System.out.println(o3.getText());
}
}
//pack2 code here
package pack2;
public class ClassB {
final String TEXT = "This is a text";
public String getText() {
return TEXT;
}
}
You don't need to create class A you can directly import pack2 and its class, method
Related
Let's say I have this code. When using an instance of ClassA, how do I pass the instance of the class to the method methodA?
public class ClassA {
public static int methodA(ClassA class) {
return 1;
}
}
//This is wrong but this is what I'm trying to do
public class Main {
public static void main(String [] args) {
ClassA classa = new ClassA();
classa.methodA(classa);
}
}
The way to achieve this is correct. Just use another name, because class is a reserved keyword for class definition
public static int methodA(ClassA instance) {
return 1;
}
Your approach works but you must not use the keyword class as a variable name. Try
public class ClassA {
public static int methodA(ClassA clazz) {
return 1;
}
}
i created two classes, each one is in a different package ... here is the class inside the main package:
package Main;
public class Main {
Public static void main(String [] args ) { .....}
}
// class in main package
class ClassS{
public int publicAtttibute;
private int pricatdAttribute;
And this is the second class in another package:
package second;
public class ClassC{
public static void doSomething(){
ClassD ref = new ClassD();
} }
But i’m not sure if this code makes the classD and it’s attributes visible to class c, can someone please clarify it
You defined an instance object of classD inside the classC which means that ClassD and all of it’s public attributes would NOT be visible to ClassC ..... if you want the ClassD to visibile but the attributes aren’t you could code it like this:
package other:
public class ClassC{
public static void visible(){
ClassD ref = new ClassD();
ref.publicAttribute = 3;
ref.privateAttribute=3;
}}
I know the following way works:
package mypackage;
public class Main{
public void oFunc(){
class Inner{
void foo(){
}
};
Inner s = new Inner();
s.foo();
}
}
Is there a way something like this could create the object and at the same time declare it?
This could not be compiled, is there a similar way to do this?
package mypackage;
public class Main{
public void oFunc(){
new class Inner{
void foo(){
}
}().foo();
}
}
With an anonymous class, you could do
new Object(){
void foo(){
}
}.foo();
but that is the only place you'll be able to use any of the members declared inside the class body.
You can have it like anonymous class as follow:
public class Main{
public void oFunc(){
new Object(){
void foo(){
}
}.foo();
}
}
Let's say I have 3 classes
class1, class2 and class3.
How can I have it that class1 can only get instantiated by class2 (class1 object = new class1()) but not by class3 or any other class?
I think it should work with modifiers but I am not sure.
I want to rename your classes to Friend, Shy and Stranger. The Friend should be able to create Shy, but Stranger should not be able to.
This code will compile:
package com.sandbox;
public class Friend {
public void createShy() {
Shy shy = new Shy();
}
private static class Shy {
}
}
But this code won't:
package com.sandbox;
public class Stranger {
public void createShy() {
Friend.Shy shy = new Friend.Shy();
}
}
In addition, if we create a new class called FriendsChild, this won't compile either:
package com.sandbox;
public class FriendsChild extends Friend {
public void childCreateShy() {
Shy shy = new Shy();
}
}
And this naming convention makes sense when you think about it. Just because I'm a friend of someone doesn't mean my child knows them.
Notice that all these classes are in the same package. As far as I can understand, this is the scenario you're looking for.
Another option besides making the constructor protected:
Make the class1 constructer private
Make a public static factory method that requires a valid instance of class2 inorder to return an instance of class1
Make class1 inner class of class2.
Update
make protected constructor and put the eligible class in same package, if you want any class outside of package to construct this instance then you need that class to extend ClassA in this example,
if you restrict it then go for default access specifier
package com.somthing.a;
public class ClassA {
ClassA() {
super();
// TODO Auto-generated constructor stub
}
}
package com.something.a;
public class ClassB {
public static void main(String[] args) {
new ClassA();//valid
}
}
package com.something.c;
public class ClassC {
public static void main(String[] args) {
new ClassA();// invalid
}
}
In Java, is there any way to use the JDK libraries to discover the private classes implemented within another class? Or do I need so use something like asm?
Class.getDeclaredClasses() is the answer.
I think this is what you're after: Class.getClasses().
package com.test;
public class A {
public String str;
public class B {
private int i;
}
}
package com.test;
import junit.framework.TestCase;
public class ReflectAB extends TestCase {
public void testAccessToOuterClass() throws Exception {
final A a = new A();
final A.B b = a.new B();
final Class[] parent = A.class.getClasses();
assertEquals("com.test.A$B", parent[0].getName());
assertEquals("i" , parent[0].getDeclaredFields()[0].getName());
assertEquals("int",parent[0].getDeclaredFields()[0].getType().getName());
//assertSame(a, a2);
}
}