#Java Classes

24 messages · Page 1 of 1 (latest)

forest wyvern
#
  
    public void a1 ( ) { 
    } 
 
    private void a2 ( ) { 
    } 
} 
 
class B { 
 
    private A objA = new A ( ); 
 
    public void b1 ( ) { 
        // Call to method a1. This works. 
        objA.a1 (); 
    } 
 
    void b2 ( ) { 
        // Call to method a2. This will give compile error. 
        objA.a2 (); 
    } 
}```


Does something like this work? Im Compiling my Code on a debian machine using Java 11. The Task i was giving implies that thats the structure they want to have. But having this gives me a "Class B is public should be declared in a file named Class B.java(The file is named "A.java"). After looking through some Forum entries thats what someone suggested to the question how to use one class inside another, so im a bit confused since i thought seperating those is the only way.
nimble marlin
#

It's convention to only have 1 class per file as Java loads files based on the class name (ex accessing the A class will load the A.java file).

#

I think if you make the A class public class A it'll stop yelling at you

forest wyvern
#

both of them are already

nimble marlin
#

Already what?

forest wyvern
#

already public classes

#

i can paste the code inside if u want

nimble marlin
#

You can't have two public classes in one file

#

Java can't find a class that doesn't match the file name.

#

So if the class name doesn't match the file name it must be private

forest wyvern
#

So a structe like this;

public class B4A1 {
public static void main(String[] args) {...}
public static int heapSelect(int[] arr, int k) {...}
public static int heapSelectFast(int[] arr, int k) {...}
}
public class MaxHeap {
public MaxHeap(int capacity) {...}
public int getSize() {...}
public int getCapacity() {...}
public int[] getValues() {...}
public void add(int value) throws IllegalStateException {...}
public int extractMax() throws IllegalStateException {...}
public int peekMax() throws IllegalStateException {...}
public void maxHeapify(int i) {...}
}

basilcy implies two seperate files?

nimble marlin
#

Yes

rancid ocean
#

Yeah split each to a file with the name of the class as zech mentioned

forest wyvern
#

its really confusing since i shouldnt have MAxHeap access inside b4a1 without extending it right ?

nimble marlin
#

You can't have two top level public classes in Java

#

In one file

forest wyvern
#

If i want to creat a maxheap object inside b4a1

#

how do i tell b4a1 what a maxheap object is? By doing class b4a1 extends maxheap ?

nimble marlin
#

Do you want b4a1 to be a maxheap or do you want it to have a maxheap?

forest wyvern
#

i want it to have a maxheap

rancid ocean
#

you import the class, and then you can create one Maxheap heap = new MaxHeap(capacity);

nimble marlin
#

Extends is inheritance so that would be an "is a" relationship. b4a1 is a maxheap when it extends maxheap.

An "has a" relationship is just gonna be MaxHeap heap = new MaxHeap(10)

forest wyvern
#

I see, it works. Thanks for your time guys ! 🙂