Android java class example

Android java class example

You might have already heard many times JAVA is an Object Oriented Programming which simply means coding in JAVA constantly involve classes and objects. In other words coding in JAVA is not possible without object and class. Even the smallest Hello world program requires declaration of class and method work on object. So let’s understand these two concepts which are really very important in JAVA.

Definition of Class and Object:

Class: The concept of class comes into role when we see certain type of objects or things around us and the common idea or a blueprint behind this type of objects is called Class. In other words class is a properties behind each of the objects or things possess.

For example: Consider you have iPhone, Samsung and Sony devices and you want to represent them in JAVA. To do that you first need to find out what can be the blueprint behind these devices. And here blueprint can be a Mobile because they all are a type of Mobile. So Mobile is a class which can represent iPhone, Samsung and Sony devices here.

How To Declare Class in JAVA:

Important Note: Class is a representation of similar types of objects or it is a implementation of encapsulation.

  • In terms of java, a class is type declaration means when you want to define a specific type of data for special use then it can be easily obtained with the help of class.
  • Basically, class is blueprint for specific type. In the human language across the world, if you count, there are lots of classes e.g. car, bank, bird, student, employee etc. are very simple example to understand.
  • Class is created with the word class when you want to define it.
    Ex: class Mobile<>
  • Class itself consists of various methods and variable.
  • To call upon class objects of other classes there must be main method with static keyword. Because with the static word method will be called even if you do not create its objects.
  • Classes are provided with special access modifiers that are default, public, private and protected.

Object: Object is an instance of class. Understanding the concept of object is lot easier when considering real life examples around us because the concept is actually based on real life objects. So just look around yourself and you will find yourself surrounded with lots of objects which has a certain characteristics and behaviors.

For example: Your mobile, it’s an example of object which has a lots of characteristics like color, RAM, camera etc and behaviors like calling, messaging etc.

How To Create Object:

Suppose Mobile is the class for which we want to create object name abhi. Below is the code:

//Object abhi is declared Mobile abhi; //Object is created using new keyword abhi = new Mobile();

Alternatively you can create object in one line:

  • Object can be defined as state, behavior of class.
  • Objects are the instance variable of class.
  • Objects are stored through references.
  • Objects are created with new keyword in java. Ex: Mobile abhi = new Mobile();
  • Every object has its own memory.
  • Objects of class of same type can interact with each other means you can pass your message from one object to other.
Читайте также:  Find b rfind python

Important Note: Objects are run time entity. They are always getting a memory at run time.

Important Note: Object are created from a class and methods or actions are performed on object.

Real Life Example Of Class And Objects:

Here in this example we will display the details of Mobile which three person Abhishek, Rahul and Ravi own. To do that in JAVA first we will create a class Mobile, declare fields for mobile specs (i.e brand, color, camera) and initialized constructor.

Then in main method we will three object for Mobile class. Each object will have the specification details of Mobile which he owns. And finally using System.out.println() method we display the details:

//Class Mobile class Mobile < String brand, color; int camera; //Constructor initialized public Mobile(String brand, String color, int camera)< this.brand = brand; this.color = color; this.camera = camera; >public static void main(String args[]) < //Object created Mobile abhishek = new Mobile("iPhone","Gold",8); Mobile rahul = new Mobile("Samsung","White",13); Mobile ravi = new Mobile("Nexus","Black",8); //Smartphone details displayed for each user System.out.println("Abhishek own " + abhishek.brand +" "+ abhishek.color + " color smartphone having "+ abhishek.camera+ "MP"+ " camera"); System.out.println("Rahul own " + rahul.brand +" "+ rahul.color + " color smartphone having "+ rahul.camera+ "MP"+ " camera"); System.out.println("Ravi own " + ravi.brand +" "+ ravi.color + " color smartphone having "+ ravi.camera+ "MP"+ " camera"); >>
Abhishek own iPhone Gold color smartphone having 8MP camera Rahul own Samsung White color smartphone having 13MP camera Ravi own Nexus Black color smartphone having 8MP camera

Another Program With Explanation To illustrate The Concept Of Class And Object:

Here in this program class ClassInJava is taken to create data type of student who is having Name, Roll number, Fathers’ Name, Contact Number, Address as fields. As from the name it is clear that these are of different primitive data type and all these are taken together in a class.

Here declaration of constructor of the class is used which initialize the object with predefined values which are passed at the creation of object.

And next class is ObjectOfClass which is used to create the object of first class. So the main method with the static written in ObjectOfClass. And this is also good prevention to declare first letter of each word of class name as capital.

public class ObjectOfClass < /**Simple illustration of how to create an object of given class and how it works */ public static void main(String[] args) < //Object of class ClassInJava ClassInJava object=new ClassInJava("Mr. Abhishek",123,"Mr. Sulekh", "+1-8745733445","#321, South Street, No-3, Ontario"); System.out.println("Student Name is: " + object.name); System.out.println("Roll Number is: " + object.rollno); System.out.println("Fathers' Name is: "+ object.fathername); System.out.println("Contact Number is: "+ object.contactno); System.out.print("Student Address is: "+ object.address); >>
Student Name is: Mr. Abhishek Roll Number is: 123 Fathers' Name is: Mr. Sulekh Contact Number is: +1-8745733445 Student Address is: #321, South Street, No-3, Ontario

Important Points about class and object:

  • The word “Class” came from simula language.
  • Class is blueprint for an entity.
  • In class there are variables and methods.
  • The access modifiers like public, private and protected used in different situation.
  • Objects represent the state and behavior of class.
  • Object is just a memory area or a buffer in heap area in which all the instance data members are getting a memory.
  • In Java “new” operator is only used for allocating a memory for an object only.
  • Memory associated with object is automatically collected by garbage collector.
  • Class with main method having static keyword is mandatory to call upon the object of other classes.
  • All the objects are sharing a same memory location for each static data members.
Читайте также:  Bubbling events in javascript

History about word “Class”: Aristotle was initially consider deeply the concept of type. Simula is language used for simulation of “bank teller problem”. There were lots of object like customer account, bank detail, employee detail and many more. So all these objects were put together in “classes of objects”. And hence the word class was invented for programming languages. That was main origin of word class.

Источник

Android tutorial — How to Create New Java Class in Android Studio — android studio tutorial

 What is the difference between Class and Object in Java

drawable in android drawable in android studio how to convert drawable to bitmap in android android shape drawable android drawable selector android drawable xml drawable android studio android r drawable android bitmap drawable android res drawable android custom drawable android vectordrawable android drawable images android resources drawable drawable android xml android drawable scale xml drawable android studio drawable xml bitmap drawable android android drawable to bitmap android drawable size bitmap from drawable draw drawable on canvas android drawable example drawable folder in android studio drawable folder in android android drawable download java for android development java code in android studio java studio android java app android studio java tutorial java programming app for android how to make an android app with java java in android studio android java programming for beginners java android app java application android download java for android studio java app android install java apps on android android java applet java to android mainactivity java android studio how to import java class in android studio how to create a class in android android studio .class files android classes and methods how to import a class in android studio classes in android programming android studio add existing java file android classes for beginners android phones best android phone android apps android developer android update android android sdk android versions android emulator kodi app poweramp android app development apps for android android software android development android apps download android app store android api developer android android programming eclipse android android studio tutorial android development tutorial android tutorial android service google android android download android device android video what is android android system

    INTERVIEW TIPS
  • Final Year Projects
  • HR Interview Q&A
  • GD Interview
  • Resume Samples
  • Engineering
  • Aptitude
  • Reasoning
  • Company Questions
  • Country wise visa
  • Interview Dress Code CAREER GUIDANCE
  • Entrance Exam
  • Colleges
  • Admission Alerts
  • ScholarShip
  • Education Loans
  • Letters
  • Learn Languages

World’s No 1 Animated self learning Website with Informative tutorials explaining the code and the choices behind it all.

Источник

How To Create New Java Class In Android Studio

In android development, the java class is commonly used to implement business logic processes such as database access, send emails, send short messages etc. We always use one java class to encapsulate some related functions.

So we can think java class as the Model role in the MVC pattern. This example will show you how to create a new java class in android studio, and we will create a SMSBean java class that will implement short message send and receives functions.

1.How To Create New Java Class In Android Studio.

  1. Launch Android Studio, create a new project. You can read the article How To Create New Android Studio Project to learn more.
  2. After that right-click the default package in the android studio Project View’s Android subview, then click the New —> Java Class menu item.
  3. You can also click the New —> Package menu item to create a new package or sub-package under exist package. In this example, we create a sub-package model under the existing com.dev2qa.example package.
  4. Then there will popup a dialog that will let you input java class-related information. Input class name SMSBean in the Name input text box, select Class in the Kind drop-down list, do not input any superclass, do not implement any interface. Input package name value such as com.dev2qa.example.model. Click the OK button.
  5. After clicking the OK button, the java class SMSBean has been created successfully. We will add two methods in it, one is sendSMS(), the other is receiveSMS(), please see code comments for detail.
Читайте также:  List from array php

2. Java Class SMSBean Usage Example.

This example will use SMSBean as a short messages manager object, it has two methods sendSMS() and receiveSMS().

The user interface of this example is not complex, there will have two buttons on the screen one is used to send short messages, the other is used to receive short messages. When you click any of the two buttons, an AlertDialog will pop up showing some messages.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/VhM3qIQkuD8

So if you want to run this example, you should create a new activity and layout XML files to implement the user interface. You can read the below articles if you do not know how to create activity and use event listener.

This example includes below core files.

2.1 SMSBean.java

package com.dev2qa.example.model; import android.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import com.dev2qa.example.SMSManagerActivity; public class SMSBean < // This variable will be injected by SMSBean caller, it will be used to show AlertDialog. private SMSManagerActivity sourceActivity; public SMSManagerActivity getSourceActivity() < return sourceActivity; >public void setSourceActivity(SMSManagerActivity sourceActivity) < this.sourceActivity = sourceActivity; >// Send short message. public void sendSMS(String msg) < if(this.getSourceActivity()!=null) < // Create a AlertDialog object. AlertDialog dialog = new AlertDialog.Builder(sourceActivity).create(); String showMsg = "Short message '" + msg + "' has been sent."; // Set AlertDialog show message. dialog.setMessage(showMsg); // Show AlertDialog. dialog.show(); System.out.println(showMsg); >else < System.out.println("Source activity is null."); >> // Receive short message. public void receiveSMS() < if(this.getSourceActivity()!=null) < // Create a AlertDialog object AlertDialog dialog = new AlertDialog.Builder(sourceActivity).create(); String showMsg = "Some short messages has been received."; // Set AlertDialog show message dialog.setMessage(showMsg); // Show AlertDialog. dialog.show(); System.out.println(showMsg); >else < System.out.println("Source activity is null."); >> >

2.2 activity_smsmanager.xml

2.3 SMSManagerActivity.java

package com.dev2qa.example; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.dev2qa.example.model.SMSBean; public class SMSManagerActivity extends AppCompatActivity < // Declare SMSBean as a final static variable. public static final SMSBean smsBean = new SMSBean(); @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_smsmanager); // set this object in smsBean, it will be used to show send / receive message dialog. smsBean.setSourceActivity(this); // Get send short message button. Button sendSmsBtn = (Button)this.findViewById(R.id.btnSendSms); // Register an OnClickListener object what listen to sendSmsBtn on click event. sendSmsBtn.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < // If sendSmsBtn is clicked, then call smsBean sendSMS method to send short message. smsBean.sendSMS("Hello Android World :)"); >>); // Get receive short message button. Button receiveSmsBtn = (Button)this.findViewById(R.id.btnReceiveSms); // Register an OnClickListener object what listen to receiveSmsBtn on click event. receiveSmsBtn.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < // If receiveSmsBtn is clicked, then call smsBean receiveSMS method to receive coming in short messages. smsBean.receiveSMS(); >>); > >

Источник

Оцените статью