What is equivalent to vb.net 'friend' keyword in java?

I want to program the inner (in C #) class, I used the keyword "Friend" in vb.Net. Now I want to do the same in Java. What is equivalent?

Friend Class NewClass End Class 
+4
source share
4 answers

There is no friend equivalent in Java. The best thing you can do is put two classes in the same package and make the members of the same class private (i.e. Without public , private or protected ) to make them available to the other.

+4
source

In Java, there is no keyword or equivalent to a friend. When I converted the VB code:

 Friend Class NewClass End Class 

to C # code, the resulting conversion:

 internal class NewClass { } 

So, to make the equivalent Java code two things you need to do:

1. Save the class in the same package where you want to access it. 2nd declare a class without an access modifier:

 class NewClass { } 
+1
source

Inner C # is equivalent to the default Java scope (which is its own scope).

Java has no internal ones.

0
source

In Visual Basic.NET, the Friend keyword talks about accessibility. In C #, the equivalent keyword is internal .

There is no such keyword in Java, but you can effectively get the same visibility in relation to the package area, leaving the access specifier class in the description of the declaration.

Taken from a comparative comparison between C # and Java versions of the same class declarations (I also added a Visual Basic version for completeness), pay attention to class B declarations, as well as AY , BY , CY and DY :

Visual Basic Version:

 Public Class A Public Shared X As Integer Friend Shared Y As Integer Private Shared Z As Integer End Class Friend Class B Public Shared X As Integer Friend Shared Y As Integer Private Shared Z As Integer Public Class C Public Shared X As Integer Friend Shared Y As Integer Private Shared Z As Integer End Class Private Class D Public Shared X As Integer Friend Shared Y As Integer Private Shared Z As Integer End Class End Class 

C # Version:

 public class A { public static int X; internal static int Y; private static int Z; } internal class B { public static int X; internal static int Y; private static int Z; public class C { public static int X; internal static int Y; private static int Z; } private class D { public static int X; internal static int Y; private static int Z; } } 

Java version:

 public class A { public static int X; static int Y; private static int Z; } class B { public static int X; static int Y; private static int Z; public class C { public static int X; static int Y; private static int Z; } private class D { public static int X; static int Y; private static int Z; } } 

See also this comparison of Visual Basic and C # for reference.

0
source

All Articles