Updated March 29, 2023
Introduction to Kotlin Internal
Kotlin internal is one of the access modifiers and it is used to declare the datas and it is visible only inside a module. It contains a set of kotlin files and it is compiled together with the modules. It may be the maven, Gradle sets and Ant tasks which is having the files generated or any other IntelliJ IDEA module. Internal is the alternative idea when compared to the java package like private also Kotlin internal means that any client or user which is inside of this module sees the declaring classes and their internal attributes.
Syntax
In general, kotlin internal is one of the access modifier types and it is used to declare the datas the inside of the kotlin modules. It has some similar and alternative ideas when compared to the kotlin private modifier. But the module datas are represented using the group of files that are compiled together.
internal class name{
val varname;
--some kotlin codes--
}
internal val name1 = values
The above codes are the basic syntax for declaring the internal keyword and access modifier used in the kotlin language. In the kotlin language internal-only has an effect across several modules in the code.
Working of Kotlin Internal
- Generally in programming language access modifier is the main role of the application code it will be validated and decided for which users are accessing & viewing the specified datas in the code. Like that public, private, protected, and internal is the new keyword and access modifier used in the kotlin language. Internal modifier is used only in the kotlin language it can be visible only that the members are visible within the same module and also more specifically the module is the set of kotlin files that are compiled together.
- Whenever develop the projects we make various modules in the project that can be separated each other like login and registration modules is the basic example for the application. So we decided for the modules contain a set of kotlin files that can be compiled together with the single packages. If we are using the internal modifier in the declaration, then it will be visible everywhere of the specified module. And also internal modifier is more benefits other than the other modules that may be more than one module in the project. So that we can use a public modifier other than that of the internal and other modifiers.
Examples of Kotlin Internal
Given below are the examples mentioned:
Example #1
Code:
internal class first {
internal fun example() {
println("Sachin is the cricket player")
println("Dhoni is the cricket player")
println("Ganguly is the cricket player")
println("Dravid is the cricket player")
println("Kumble is the cricket player")
println("Kohli is the cricket player")
println("Sehwag is the cricket player")
println("Dhawan is the cricket player")
println("Rohit is the cricket player")
println("Zaheer is the cricket player")
println("Bumbra is the cricket player")
println("Yuvaraj is the cricket player")
}
}
public class Second {
internal val int = 10
internal fun demo() {
println("January is the first month")
println("February is the second month")
println("March is the third month")
println("April is the fourth month")
println("May is the fifth month")
println("June is the sixth month")
println("July is the seventh month")
println("August is the eight month")
println("September is the ninth month")
println("October is the tenth month")
println("November is the eleventh month")
println("December is the twelfth month")
}
}
fun main(args: Array<String>) {
println("Welcome To My Domain its the first example that related to the kotlin internal")
var first = Second()
println("Have a Nice Day users, your month details are: "+first.demo())
var sec= first()
println("Have a Nice Day users, the player details are: "+sec.example())
val x = setOf("Television", 1, "Personal Computer", 2, "Laptop",3,"Tablet",4,"Microwave Oven",5,"Fridge",6,"Washing-machine",7,"Ironbox",8,"Aircooler",9,"Air-Conditioner",10,"Mobiles",11,"Speakers",12,"Bluetooth",13,"Smartwatch",14)
val y = setOf<String>("Tamil", "English", "Computer Science","Physics","Chemistry","Biology","Mathematics")
println(x)
println(y)
}
Output:
Explanation:
- In the above example, we created the internal class first and will declare the method like an example using the internal keyword. In that, we can create the datas like printing the cricket players on using the above method. So it will access only the internal class package and specified method for adding the set of files into the single kotlin files. We can repository the above codes in the build tool like Maven, Gradle, and ant, etc.
- Next, we can create the class using the public modifier and will create the method demo() using internal and variable called int using the internal keyword. In the main method, we can create the object for the above two classes and will call the each method in a separate instance. Here additionally we can create the datas using the set collection interface for printing the datas without duplication.
Example #2
Code:
private class Second {
private val x = 13
internal val int = 16
internal fun display() {
}
fun show()
{
println(int)
println("Customer has updated to proceed with the SQL upgrade on Monday 26th and we requested to perform the activity after 6 Pm Vietnam time and its pending for their approval.")
println("Customer has updated to perform the activity")
println("We checked with our IT team and they have requested whether this upgrade can happen after the working hours i.e after 6 PM on Monday?")
}
}
fun main(args: Array<String>){
println("Welcome To My Domain its a second example that related to the kotlin internal concepts")
var Second = Second()
Second.show()
Second.display()
}
Output:
Explanation:
- In the second example, we used and declared the class as a private modifier and initialize the variable value as private and internal.
- Here we declared the methods like the show(), display() in private class and we utilised the methods in the main class. We created the object of the private class and called the method like the show(), display() using the private class instance.
Conclusion
In kotlin language, we used a lot of default classes, methods, variables and types, access-modifiers etc. Using these we can able to create the application task and files wherever it requires from the user needs. The datatype and access-modifiers are the restrict-edit types to access the values in the kotlin file.
Recommended Articles
This is a guide to Kotlin Internal. Here we discuss the introduction, syntax, and working of internal kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –